-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathextractExrLayers.py
More file actions
318 lines (239 loc) · 10.9 KB
/
extractExrLayers.py
File metadata and controls
318 lines (239 loc) · 10.9 KB
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
316
317
318
#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/07/2019.
#Modified by alexandre bon on 2021/09/02: reliable appGui retrievement / reduce spacing / add postage stamps
# TODO remove unused channels (with a merge node)
import os
import string
import NatronEngine
from NatronGui import *
from Python_GUI.postageStamp.postageStamp import * # from the community scripts
# EXTRACT EXR LAYERS #
def extractExrLayers():
layer_spacing = 20
nodes_spacing_y = 10
mergeOffsetY = 50
constantOffsetX = 100
# get current Natron instance running in memory #
# app = natron.getGuiInstance(0) # more reliable method below
#myNoGUIApp = natron.getActiveInstance()
#myNoGUIAppID = natron.getActiveInstance().getAppID()
#myAppGui = natron.getGuiInstance(myNoGUIAppID)
#app = myAppGui
myApp = natron.getActiveInstance()
app = natron.getGuiInstance( myApp.getAppID() )
# get selected nodes #
selectedNodes = app.getSelectedNodes()
# check if at least one node has been selected #
if len(selectedNodes) == 0:
warning = natron.warningDialog("Extract EXR","Select at least one node. \n This script extract multilayer EXR files \n ")
else :
# create dialog window #
dialog = app.createModalDialog()
# set dialog title #
dialog.setWindowTitle("ExtractEXR")
# set dialog margins #
dialog.setContentsMargins(0, 0, 10, 10)
# set window size #
dialog.resize( 400, 100 )
################################################
# UI CREATION #
################################################
line01Param = dialog.createStringParam("sep01","")
line01Param.setType(NatronEngine.StringParam.TypeEnum.eStringTypeLabel)
line02Param = dialog.createStringParam("sep02","")
line02Param.setType(NatronEngine.StringParam.TypeEnum.eStringTypeLabel)
line02Param.set("Your reader will be expanded into each of its layers:")
prefixUnderscoreParam = dialog.createBooleanParam("choice03","add _ prefix to output nodes")
prefixUnderscoreParam.set(True)
removeChannelsParam = dialog.createBooleanParam("choice04","Keep only RGBA for each layer")
removeChannelsParam.set(True)
sizeMultParam = dialog.createIntParam("int01","Size Multiplyer")
sizeMultParam.set(10)
# Refresh UI #
dialog.refreshUserParamsGUI()
################################################
# SHOW UI #
################################################
# if user press OK #
dialogResult = dialog.exec_()
if dialogResult == True :
# retrieve user choices #
prefixUnderscore = prefixUnderscoreParam.get()
removeChannels = removeChannelsParam.get()
sizeMult = sizeMultParam.get()
sizeMult = min (40, max(4 , sizeMult))
layer_spacing *= sizeMult
nodes_spacing_y *= sizeMult
# cycle through every selected node #
for currentNode in selectedNodes:
# get node ID #
currentID = currentNode.getPluginID()
# check if selected node is a 'Read' node #
if currentID == 'fr.inria.built-in.Read':
# get 'Read' node position #
readPosition = currentNode.getPosition()
###################
# CREATE ROOT DOT #
###################
# create root dot #
rootDot = app.createNode('fr.inria.built-in.Dot')
# set root dot position #
rootDot.setPosition(readPosition[0] + 45 , readPosition[1] + 300)
# connect root dot to 'Read' #
rootDot.connectInput(0,currentNode)
# get root dot position #
rootDotPosition = rootDot.getPosition()
# get all available layers in EXR #
layersList = currentNode.getParam('outputLayer').getOptions()
# sort list alphabetically #
list.sort(layersList)
backdropLength = len(layersList)
# initialize counter #
channelCounter = 0
# cycle through every layer #
for choice in layersList:
if choice != 'Color.RGBA':
# full layer name #
fullLayerName = choice
# layer name #
layerName = os.path.splitext(choice)[0]
print (layerName)
# layer channels (RGBA,RGB,XYZ,UV,A,Z) #
layerChannels = os.path.splitext(choice)[1]
# remove '.' from channels #
layerChannels = layerChannels.replace('.','')
# create the first 'Shuffle' node #
if channelCounter == 0 :
###################
# CREATE SHUFFLE #
###################
# create a 'Shuffle' node #
newShuffle = app.createNode('net.sf.openfx.ShufflePlugin')
# set 'Shuffle' node position #
newShuffle.setPosition(rootDotPosition[0] - 45 , rootDotPosition[1] + nodes_spacing_y )
# connect 'Shuffle' to root dot #
newShuffle.connectInput(0,rootDot)
# create constant to use for the remove layers operation
if removeChannels:
sizeRefName = newShuffle.getScriptName()
myConstant = app.createNode('net.sf.openfx.ConstantPlugin')
myConstant.setPosition(rootDotPosition[0] - 200 , rootDotPosition[1])
myConstant.getParam('extent').set('size')
bottomLeftParam = myConstant.getParam('bottomLeft')
sizeParam = myConstant.getParam('size')
storeExpression =sizeRefName+".getOutputFormat().bottom()"
bottomLeftParam.setExpression(storeExpression,False, dimension=0 )
storeExpression =sizeRefName+".getOutputFormat().left()"
bottomLeftParam.setExpression(storeExpression,False, dimension=1 )
storeExpression=sizeRefName+".getOutputFormat().width()"
sizeParam.setExpression(storeExpression,False, dimension=0)
storeExpression=sizeRefName+".getOutputFormat().height()"
sizeParam.setExpression(storeExpression,False, dimension=1 )
# create merge for remove operation
myMerge = app.createNode('net.sf.openfx.MergePlugin')
myMerge.connectInput(1,newShuffle)
myMerge.connectInput(0,myConstant)
myMerge.setPosition(newShuffle.getPosition()[0] , newShuffle.getPosition()[1]+mergeOffsetY)
# create a 'Backdrop' #
newBackdrop = app.createNode('fr.inria.built-in.BackDrop')
newBackdrop.setPosition(rootDotPosition[0] - layer_spacing - constantOffsetX , rootDotPosition[1] - 60)
newBackdrop.setSize( (backdropLength +1 )*layer_spacing + constantOffsetX , nodes_spacing_y*4 + 20)
newBackdrop.setColor(0.5, 0.35, 0.12)
# create all the other 'Shuffle' nodes #
if channelCounter != 0 :
# create a new dot #
newDot = app.createNode('fr.inria.built-in.Dot')
# set root dot position #
newDot.setPosition(rootDotPosition[0] + layer_spacing , rootDotPosition[1])
# connect root dot to previous dot #
newDot.connectInput(0,rootDot)
# replace old Dot position value
rootDotPosition = newDot.getPosition()
###################
# CREATE SHUFFLE #
###################
# create a 'Shuffle' node #
newShuffle = app.createNode('net.sf.openfx.ShufflePlugin')
# set 'Shuffle' node position #
newShuffle.setPosition(rootDotPosition[0] - 45 , rootDotPosition[1] + nodes_spacing_y )
# connect 'Shuffle' to root dot #
newShuffle.connectInput(0,newDot)
if removeChannels:
# create merge for remove operation
myMerge = app.createNode('net.sf.openfx.MergePlugin')
myMerge.connectInput(1,newShuffle)
myMerge.connectInput(0,myConstant)
myMerge.setPosition(newShuffle.getPosition()[0] , newShuffle.getPosition()[1]+mergeOffsetY)
# ADD POSTAGE STAMP
stamp_spacing_y = 100
hideInput = False
preview = True
if prefixUnderscore :
nodeLabel = '_'+ layerName
else :
nodeLabel = layerName
if removeChannels:
app.selectNode(myMerge,True)
else:
app.selectNode(newShuffle, True)
postageStamp(stamp_spacing_y , hideInput , preview , nodeLabel)
##########################
# SET SHUFFLE PARAMETERS #
##########################
# set 'Shuffle' label #
newShuffle.setLabel(str(layerName))
# enable preview (deprecated with postage stamp addition)#
#newShuffle.getParam('enablePreview').setValue(1)
#newShuffle.getParam('enablePreview').setValue(0)
#newShuffle.getParam('enablePreview').setValue(1)
# set 'Shuffle' color #
newShuffle.setColor(1, 0.5, 0.15)
# modified by AB to correctly handle non RGB channels
# if layer has 4 channels #
if len(layerChannels) == 4 :
newShuffleValue = 'B.' + str(layerName) + '.' + str(layerChannels[0:1])
newShuffle.getParam('outputR').set(newShuffleValue)
newShuffleValue = 'B.' + str(layerName) + '.' + str(layerChannels[1:2])
newShuffle.getParam('outputG').set(newShuffleValue)
newShuffleValue = 'B.' + str(layerName) + '.' + str(layerChannels[2:3])
newShuffle.getParam('outputB').set(newShuffleValue)
newShuffleValue = 'B.' + str(layerName) + '.' + str(layerChannels[3:4])
newShuffle.getParam('outputA').set(newShuffleValue)
# if layer has 3 channels #
if len(layerChannels) == 3 :
newShuffleValue = 'B.' + str(layerName) + '.' + str(layerChannels[0:1])
newShuffle.getParam('outputR').set(newShuffleValue)
newShuffleValue = 'B.' + str(layerName) + '.' + str(layerChannels[1:2])
newShuffle.getParam('outputG').set(newShuffleValue)
newShuffleValue = 'B.' + str(layerName) + '.' + str(layerChannels[2:3])
newShuffle.getParam('outputB').set(newShuffleValue)
newShuffle.getParam('outputA').setValue(4)
# if layer has 2 channels #
if len(layerChannels) == 2 :
newShuffleValue = 'B.' + str(layerName) + '.' + str(layerChannels[0:1])
newShuffle.getParam('outputR').set(newShuffleValue)
newShuffleValue = 'B.' + str(layerName) + '.' + str(layerChannels[1:2])
newShuffle.getParam('outputG').set(newShuffleValue)
newShuffle.getParam('outputB').setValue(4)
newShuffle.getParam('outputA').setValue(4)
# if layer has 1 channel #
if len(layerChannels) == 1 :
newShuffleValue = 'B.' + str(layerName) + '.' + str(layerChannels[0:1])
newShuffle.getParam('outputR').set(newShuffleValue)
newShuffle.getParam('outputG').setValue(4)
newShuffle.getParam('outputB').setValue(4)
newShuffle.getParam('outputA').setValue(4)
# if layer has 0 channel #
if len(layerChannels) == 0 :
newShuffleValue = 'B.' + str(choice)
newShuffle.getParam('outputR').set(newShuffleValue)
newShuffle.getParam('outputG').setValue(4)
newShuffle.getParam('outputB').setValue(4)
newShuffle.getParam('outputA').setValue(4)
# increase counter #
channelCounter += 1
app.setSelection(selectedNodes)
# launch inside Natron Script Editor
# extractExrLayers()