-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathflipbook.py
315 lines (236 loc) · 10.7 KB
/
flipbook.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#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 22/06/2019.
import os
import sys
import subprocess
from NatronGui import *
from PySide.QtGui import *
import NatronEngine
# RENDERS AND PLAYS THE SELECTED NODE IN AN EXTERNAL VIEWER #
def flipbook():
# creates dialog window #
app = natron.getGuiInstance(0)
dialog = app.createModalDialog()
# set dialog title #
dialog.setWindowTitle("Flipbook")
# 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)
# creates In/Out user parameters #
firstFrame = dialog.createIntParam("firstFrame","In :")
defaultFirstFrame = app.getProjectParam('frameRange').get()[0]
firstFrame.set(defaultFirstFrame)
lastFrame = dialog.createIntParam("lastFrame","Out :")
defaultLastFrame = app.getProjectParam('frameRange').get()[1]
lastFrame.set(defaultLastFrame)
lastFrame.setAddNewLine(False)
line03 = dialog.createStringParam("sep03","")
line03.setType(NatronEngine.StringParam.TypeEnum.eStringTypeLabel)
sep01 = dialog.createSeparatorParam("line01","")
# creates formats list #
formatList = dialog.createChoiceParam("choice01","Format : ")
entries = [ ("exr", ""),("jpg", ""),("png", "") ]
formatList.setOptions(entries)
formatList.setDefaultValue("exr")
formatList.restoreDefaultValue()
# creates players list #
playerList = dialog.createChoiceParam("choice02","Players : ")
playerList.setAddNewLine(False)
entries = [ ("DJV", ""),("mrViewer", ""),("PDPLAYER", "") ]
playerList.setOptions(entries)
playerList.setDefaultValue("DJV")
playerList.restoreDefaultValue()
sep02 = dialog.createSeparatorParam("line02","")
# Refresh UI #
dialog.refreshUserParamsGUI()
# code executed when OK button is pressed #
if dialog.exec_():
# retrieves values entered by user #
newFirstFrame = dialog.getParam("firstFrame").getValue()
newLastFrame = dialog.getParam("lastFrame").getValue()
counter = 0
selectedNodes = app.getSelectedNodes()
# Gives user's home directory
myUserPath = os.path.expanduser('~')
# Gives username by splitting path based on OS
myUser = os.path.split(myUserPath)[-1]
for n in selectedNodes:
# if more than 1 node is selected, stop the process #
if len(selectedNodes) != 1 :
break
else :
# creates a diskWrite node #
parentPosition = n.getPosition()
diskWrite = app.createNode("fr.inria.built-in.Write")
# connects the Disk Cache node to the selected node, and set graph position. #
diskWrite.connectInput(0, n)
# set node position #
diskWrite.setPosition(parentPosition[0]+150,parentPosition[1])
# set node name #
parentLabel = n.getLabel()
diskWrite.setLabel('diskWrite_' + parentLabel)
# retrieve format choice #
myFormat = formatList.getValue()
extension = ''
if myFormat == 0 :
extension = 'exr'
if myFormat == 1 :
extension = 'jpg'
if myFormat == 2 :
extension = 'png'
# --------------------- check OS --------------------- #
# ---------------------------------------------------- #
# ----------------------- Linux ---------------------- #
# ---------------------------------------------------- #
if natron.isLinux() == 1 :
# get diskCachePath from Preferences #
userDiskCachePath = NatronEngine.natron.getSettings().getParam('diskCachePath').get()
# rebuild render path #
if userDiskCachePath == '':
# if diskCachePath is empty (default) #
folderPath = str(myUserPath) + '/.cache/INRIA/Natron/flipbook_Cache/' + parentLabel
else :
# if diskCachePath is set to a custom folder #
folderPath = str(userDiskCachePath) + '/flipbook_Cache/' + parentLabel
# check if output folder exists #
if not os.path.exists(folderPath):
os.makedirs(folderPath)
# rebuild full render path + filename #
myPath = str(folderPath) + '/' + parentLabel + '.######.' + str(extension)
# select which player to use #
myPlayer = playerList.getValue()
if myPlayer == 0:
viewerPath = ''.join(file( str(myUserPath) + '/.Natron/Python_GUI/flipbook/preferences/LINUX_DJV.txt'))
currentViewer = 'djv_view.sh'
viewerLabel = 'DJV'
fullViewerPath = viewerPath + currentViewer
if myPlayer == 1:
viewerPath = ''.join(file( str(myUserPath) + '/.Natron/Python_GUI/flipbook/preferences/LINUX_mrViewer.txt'))
viewerLabel = 'mrViewer'
currentViewer = 'mrViewer.sh'
fullViewerPath = viewerPath + currentViewer
if myPlayer == 2:
viewerPath = ''.join(file( str(myUserPath) + '/.Natron/Python_GUI/flipbook/preferences/LINUX_mrViewer.txt'))
viewerLabel = 'PdPlayer'
currentViewer = 'pdplayer64'
fullViewerPath = viewerPath + currentViewer
# ---------------------------------------------------- #
# ---------------------- Windows --------------------- #
# ---------------------------------------------------- #
if natron.isWindows() == 1 :
# get diskCachePath from Preferences #
userDiskCachePath = NatronEngine.natron.getSettings().getParam('diskCachePath').get()
# rebuild render path #
if userDiskCachePath == '':
# if diskCachePath is empty (default) #
folderPath = str(myUserPath) + '\\AppData\\Local\\INRIA\\Natron\\flipbook_Cache\\' + parentLabel
else :
# if diskCachePath is set to a custom folder #
folderPath = str(userDiskCachePath) + '\\flipbook_Cache\\' + parentLabel
# check if output folder exists #
if not os.path.exists(folderPath):
os.makedirs(folderPath)
# rebuild full render path + filename #
myPath = str(folderPath) + '\\' + parentLabel + '.######.' + extension
# select which player to use #
myPlayer = playerList.getValue()
if myPlayer == 0:
viewerPath = ''.join(file( str(myUserPath) + '/.Natron/Python_GUI/flipbook/preferences/WIN_DJV.txt') )
currentViewer = 'djv_view.exe'
viewerLabel = 'DJV'
fullViewerPath = viewerPath.replace('\\', '\\\\') + '\\' + currentViewer
if myPlayer == 1:
viewerPath = ''.join(file( str(myUserPath) + '/.Natron/Python_GUI/flipbook/preferences/WIN_mrViewer.txt'))
currentViewer = 'mrViewer.exe'
viewerLabel = 'mrViewer'
fullViewerPath = viewerPath.replace('\\', '\\\\') + '\\' + currentViewer
if myPlayer == 2:
viewerPath = ''.join(file( str(myUserPath) + '/.Natron/Python_GUI/flipbook/preferences/WIN_PDPLAYER.txt'))
currentViewer = 'pdplayer64.exe'
viewerLabel = 'PdPlayer'
fullViewerPath = viewerPath.replace('\\', '\\\\') + '\\' + currentViewer
# ---------------------------------------------------- #
# ---------------------- Mac OSX --------------------- #
# ---------------------------------------------------- #
if natron.isMacOSX() == 1 :
# get diskCachePath from Preferences #
userDiskCachePath = NatronEngine.natron.getSettings().getParam('diskCachePath').get()
# rebuild render path #
if userDiskCachePath == '':
# if diskCachePath is empty (default) #
folderPath = str(myUserPath) + '/.cache/INRIA/Natron/flipbook_Cache/' + parentLabel
else :
# if diskCachePath is set to a custom folder #
folderPath = str(userDiskCachePath) + '/flipbook_Cache/' + parentLabel
# check if output folder exists #
if not os.path.exists(folderPath):
os.makedirs(folderPath)
# rebuild full render path + filename #
myPath = str(folderPath) + '/' + parentLabel + '.######.' + str(extension)
# select which player to use #
myPlayer = playerList.getValue()
if myPlayer == 0:
viewerPath = ''.join(file( str(myUserPath) + '/.Natron/Python_GUI/flipbook/preferences/OSX_DJV.txt'))
currentViewer = 'djv_view.app'
viewerLabel = 'DJV'
fullViewerPath = viewerPath + currentViewer
if myPlayer == 1:
viewerPath = ''.join(file( str(myUserPath) + '/.Natron/Python_GUI/flipbook/preferences/OSX.txt'))
viewerLabel = 'mrViewer'
currentViewer = 'mrViewer.app'
fullViewerPath = viewerPath + currentViewer
if myPlayer == 2:
viewerPath = ''.join(file( str(myUserPath) + '/.Natron/Python_GUI/flipbook/preferences/OSX.txt'))
viewerLabel = 'pdplayer64.app'
currentViewer = 'PdPlayer'
fullViewerPath = viewerPath + currentViewer
# --------------------- setup 'Write' node --------------------- #
# set Write render path #
oldPath = diskWrite.getParam("filename").set(myPath)
# set Write frame range to 'Manual' #
myFrameRange = diskWrite.getParam("frameRange").set(2)
# set Write first frame to render #
myFirstFrame = diskWrite.getParam("firstFrame").set(newFirstFrame)
# set Write last frame to render #
myLastFrame = diskWrite.getParam("lastFrame").set(newLastFrame)
# launch render #
app.renderBlocking(diskWrite, newFirstFrame, newLastFrame)
fLength = len(str(newFirstFrame))
lLength = len(str(newLastFrame))
# rebuild leading zeros #
newDigits1 =''
while fLength < 6:
newDigits1 += '0'
fLength += 1
newDigits2 =''
while lLength < 6:
newDigits2 += '0'
lLength += 1
# print message in console #
fullRenderName = parentLabel + '.' + newDigits1 + str(newFirstFrame) + '-' + newDigits2 + str(newLastFrame) + '.' + str(extension)
if myPlayer == 2:
fullRenderName = parentLabel + '.' + '######' + '.' + str(extension)
os.write(1, '\n' 'Opening [ ' + fullRenderName + ' ] in ' + viewerLabel + '\n' + '\n')
# ---------------------- launch external viewer --------------------- #
# go to viewer folder #
os.chdir(viewerPath)
# Windows #
if natron.isWindows() == 1 :
fullRenderPath = str(folderPath) + '\\' + str(fullRenderName)
subprocess.Popen( [fullViewerPath, fullRenderPath] , stdin = subprocess.PIPE, stdout = subprocess.PIPE)
# Linux #
if natron.isLinux() == 1 :
fullRenderPath = str(folderPath) + '/' + str(fullRenderName)
subprocess.Popen( [fullViewerPath, fullRenderPath] , stdin = subprocess.PIPE, stdout = subprocess.PIPE)
# OSX #
if natron.isMacOSX() == 1 :
fullRenderPath = str(folderPath) + '/' + str(fullRenderName)
subprocess.Popen( [fullViewerPath, fullRenderPath] , stdin = subprocess.PIPE, stdout = subprocess.PIPE)