-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathrotoToTracker.py
141 lines (92 loc) · 4.06 KB
/
rotoToTracker.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
#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 18/07/2018.
from NatronEngine import*
from NatronGui import *
# ROTO TO TRACKER #
def rotoToTracker():
# get current Natron instance running in memory #
app = natron.getGuiInstance(0)
# we store selected node(s) in a list #
selectedNodes = app.getSelectedNodes()
# check if at least one node has been selected #
if len(selectedNodes) == 0 :
warning = natron.warningDialog("Warning","Select at least one node.")
else:
# cycle every selected node #
for node in selectedNodes :
# get node type #
nodeID = node.getPluginID()
# get node label #
nodeLabel = node.getLabel()
# get node position in the Node Graph for later use #
nodePosition = node.getPosition()
# initialize a counter #
nodeCounter = 1
# check if selected node(s) are of 'Roto' class #
if nodeID == "fr.inria.built-in.RotoPaint" or nodeID == "fr.inria.built-in.Roto" :
# grab 'Roto context' #
rotoContext = node.getRotoContext()
# get 'Base layer' in 'Roto context' #
baseLayer = rotoContext.getBaseLayer()
# get all items in 'Base layer'
allBaseLayerItems = baseLayer.getChildren()
# we initialize an offset value for new created 'Tracker' position in the Node Graph #
newNodeYPosOffset = 0
# cycle every item in 'Base layer' #
for item in allBaseLayerItems:
# get current item 'Label' #
itemName = item.getLabel()
# retrieve current 'Item' using its name #
currentBezier = rotoContext.getItemByName(itemName)
# check if 'Item' is of 'BezierCurve' class #
currentClass = type(currentBezier)
if 'BezierCurve' in str(currentClass):
pointNumber = currentBezier.getNumControlPoints()
# -----------------------------------------------------------#
# CREATE 'TRACKER' NODE #
# -----------------------------------------------------------#
# create a 'Tracker' node #
newTracker = app.createNode("fr.inria.built-in.Tracker")
# set 'Tracker' label #
newTracker.setLabel(str(itemName) + '_Tracker')
# set node position in Node Graph #
newTracker.setPosition( nodePosition[0] + 120 ,nodePosition[1] + newNodeYPosOffset )
# set 'Motion type' to 'Match-Move' #
newTracker.getParam('motionType').set('Match-Move')
# recursive creation of tracks #
trackNumber = 0
while trackNumber < pointNumber:
# get 'Tracker context' #
newTrackerContext = newTracker.getTrackerContext()
# create new 'Track' #
newTrack = newTrackerContext.createTrack()
# get current 'Track' label #
trackName = newTrack.getScriptName()
# retrieve current 'Track' #
currentTrack = newTrackerContext.getTrackByName(trackName)
currentTrack.getParam('motionModel').set('Trans.+Rot.+Scale')
# get 'currentBezier' all keyframes #
currentBezierKeyframes = currentBezier.getKeyframes()
# get number of 'currentBezier' keyframes #
keyframesNumber = len(currentBezierKeyframes)
# cycle every 'currentBezier' keyframes #
for currentTime in currentBezierKeyframes:
# get current point position at current keyframe #
currentPointPosition = currentBezier.getControlPointPosition(trackNumber,currentTime)
pointXPos = currentPointPosition[0]
pointYPos = currentPointPosition[1]
# get the 'Track'center point parameter #
trackPosition = currentTrack.getParam('centerPoint')
# set the 'Track' position to current point position #
trackPosition.set(pointXPos,pointYPos,currentTime)
# go to next 'Track' #
trackNumber += 1
newNodeYPosOffset += 100
# go to next node #
nodeCounter += 1
else:
warning = natron.warningDialog("Warning","Select Roto node(s).")
# deselect all nodes #
app.clearSelection()