-
Notifications
You must be signed in to change notification settings - Fork 24
/
backgroundRender.py
309 lines (215 loc) · 8.72 KB
/
backgroundRender.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
#WINDOWS : where natron -> trouver le dossier d'installation
#LINUX : which natron
#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 29/06/2019.
import os
import sys
import stat
import codecs
import string
import shutil
import datetime
import subprocess
import NatronEngine
from NatronGui import *
from PySide.QtGui import *
# BATCH RENDER #
def backgroundRender():
app = natron.getGuiInstance(0)
dialog = app.createModalDialog()
# set dialog title #
dialog.setWindowTitle("Batch render")
# set dialog margins #
dialog.setContentsMargins(0, 0, 10, 10)
# set window size #
dialog.setFixedSize( 340, 150 )
########################### UI CREATION ###################################
# separators #
line01 = dialog.createStringParam("line01","")
line01.setType(NatronEngine.StringParam.TypeEnum.eStringTypeLabel)
line02 = dialog.createStringParam("line02","")
line02.setType(NatronEngine.StringParam.TypeEnum.eStringTypeLabel)
# CREATE CHOICE LIST #
rangeList = dialog.createChoiceParam("rangeChoice","Frame Range : ")
rangeList.addOption('Project','')
rangeList.addOption('Custom','')
# CREATE WRITE LIST #
writeList = dialog.createChoiceParam("writeChoice","Write : ")
writeList.setAddNewLine(False)
# if a viewer exists in comp, add it to the choice list #
app.selectAllNodes()
selectedNodes = app.getSelectedNodes()
for currentNode in selectedNodes:
currentID = currentNode.getPluginID()
if currentID == 'fr.inria.built-in.Viewer':
currentLabel = currentNode.getLabel()
rangeList.addOption(str(currentLabel),'')
if currentID == 'fr.inria.built-in.Write':
currentLabel = currentNode.getLabel()
writeList.addOption(str(currentLabel),'')
app.clearSelection()
rangeList.setDefaultValue('Project')
rangeList.restoreDefaultValue()
# separators #
line03 = dialog.createStringParam("line03","")
line03.setType(NatronEngine.StringParam.TypeEnum.eStringTypeLabel)
line04 = dialog.createStringParam("line04","")
line04.setType(NatronEngine.StringParam.TypeEnum.eStringTypeLabel)
# IN parameter #
inRange = dialog.createIntParam("inRange","Start Frame : ")
# OUT parameter #
outRange = dialog.createIntParam("outRange","End Frame : ")
outRange.setAddNewLine(False)
###########################################################################
dialog.refreshUserParamsGUI()
# user press OK button
if dialog.exec_():
################################################
os.write( 1,'\n')
os.write( 1,'###############################################')
os.write( 1,'\n')
os.write( 1,'############# BACKGROUND RENDER #############')
os.write( 1,'\n')
os.write( 1,'###############################################')
os.write( 1,'\n')
os.write( 1,'\n')
# grab current project path #
projectPath = app.getProjectParam('projectPath').get()
if projectPath == '':
warning = natron.warningDialog("Warning","Save project first.")
else :
#############################################################
##################### SAVE NEW PROJECT #####################
#############################################################
# go to project path #
os.chdir(projectPath)
cwd = os.getcwd()
# grab current project name #
projectName = app.getProjectParam('projectName').get()
# grab current project name without the .ntp extension #
projectNameNoExt = os.path.splitext(projectName)[0]
# get date and time #
currentDT = datetime.datetime.now()
date = currentDT.strftime("%Y-%m-%d_%Hh-%Mmin-%Ssec")
# set new project name #
newProjectName = str(projectNameNoExt) + '_' + str(date) + '.ntp'
os.write( 1,'\n')
os.write( 1, 'Rendered project is : ' + str(newProjectName) )
os.write( 1,'\n')
os.write( 1,'\n')
# create temp folder #
tempFolder = str(projectPath) + 'bgRenderTemp'
if os.path.exists(tempFolder):
pass
else:
os.makedirs(tempFolder)
os.write( 1,'\n')
os.write( 1, 'Temp folder created : ' + str(tempFolder))
os.write( 1,'\n')
os.write( 1,'\n')
# copy current project in temp folder #
shutil.copy(projectName,tempFolder)
# rename project in temp folder #
newProjectOldName = str(tempFolder) + '/' + str(projectName)
newProjectNewName = str(tempFolder) + '/' + str(newProjectName)
os.rename(newProjectOldName,newProjectNewName)
###############################################################
################## GET NATRON INSTALL FOLDER #################
###############################################################
natronFolder = os.path.dirname((sys.executable))
###############################################################
# get range user choice #
rangeUserChoice = rangeList.get()
# 'Project' is selected #
if rangeUserChoice == 0 :
# get project frame range #
frameRange = app.getProjectParam('frameRange').get()
inFrame = frameRange[0]
outFrame = frameRange[1]
renderRange = str(inFrame) + '-' + str(outFrame)
os.write( 1,'\n')
os.write( 1,'Project frame range selected : ' + str(inFrame) + '-' + str(outFrame) )
os.write( 1,'\n')
os.write( 1,'\n')
# 'Custom' is selected #
if rangeUserChoice == 1 :
# get user frame range #
inFrame = inRange.get()
outFrame = outRange.get()
renderRange = str(inFrame) + '-' + str(outFrame)
os.write( 1,'\n')
os.write( 1, 'Custom frame range selected : ' + str(inFrame) + '-' + str(outFrame) )
os.write( 1,'\n')
os.write( 1,'\n')
# a 'Viewer' is selected #
if rangeUserChoice >1 :
# we grab selected 'viewer' #
viewerChoice = rangeList.getOption(rangeUserChoice)
# select all nodes in Node Graph #
app.selectAllNodes()
selectedNodes = app.getSelectedNodes()
# cycle through every selected nodes #
for currentNode in selectedNodes:
# get current node ID #
currentID = currentNode.getPluginID()
# if the current node's ID is of 'viewer' type #
if currentID == 'fr.inria.built-in.Viewer':
# then we grab its 'label' #
viewerLabel = currentNode.getLabel()
# select it #
myViewer = app.getViewer(viewerLabel)
# get its frame range #
currentRange = myViewer.getFrameRange()
inFrame = currentRange[0]
outFrame = currentRange[1]
renderRange = str(inFrame) + '-' + str(outFrame)
os.write( 1,'\n')
os.write( 1, str(viewerLabel) + ' frame range selected : ' + str(inFrame) + '-' + str(outFrame) )
os.write( 1,'\n')
os.write( 1,'\n')
app.clearSelection()
# get Write user choice #
writeUserChoiceIndex = writeList.get()
if writeUserChoiceIndex == '':
warning = natron.warningDialog("Warning","Create a 'Write' node first.")
else:
writeUserChoiceLabel = writeList.getOption(writeUserChoiceIndex)
app.selectAllNodes()
selectedNodes = app.getSelectedNodes()
for currentNode in selectedNodes:
currentLabel = currentNode.getLabel()
if currentLabel == writeUserChoiceLabel:
fileRenderPath = currentNode.getParam('filename').get()
app.clearSelection()
# go to temp folder #
os.chdir(tempFolder)
# Windows #
if natron.isWindows() == 1 :
# set batch file name #
batchFileName = str(projectNameNoExt) + '_' + str(date) + '.bat'
# create batch file #
with codecs.open(batchFileName, 'w+', errors="ignore") as batchRender:
# write instruction in batch file #
renderInstruction = 'START ' + str(natronFolder) + '/' + 'NatronRenderer ' + str(newProjectNewName) + ' -w ' + str(writeUserChoiceLabel) + ' ' + str(fileRenderPath) + ' ' + str(renderRange)
renderInstruction = renderInstruction.replace('/','\\')
renderInstruction = renderInstruction.replace('Program Files','"Program Files"')
batchRender.write('@echo off\n')
batchRender.write(str(renderInstruction))
# run batch file #
currentRender = subprocess.call(batchFileName)
# Linux/OSX #
if natron.isLinux() == 1 or natron.isMacOSX() == 1:
# set batch file name #
batchFileName = str(projectNameNoExt) + '_' + str(date) + '.bash'
# create batch file #
with codecs.open(batchFileName, 'w+', errors="ignore") as batchRender:
# write instruction in batch file #
renderInstruction = './' + str(natronFolder) + '/' + 'NatronRenderer ' + str(newProjectNewName) + ' -w ' + str(writeUserChoiceLabel) + ' ' + str(fileRenderPath) + ' ' + str(renderRange)
batchRender.write('#!/bin/bash\n')
batchRender.write(str(renderInstruction))
# make bash file executable #
os.chmod(batchFileName,stat.S_IRWXU)
# run batch file #
currentRender = subprocess.call(batchFileName)