-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprojectExplorer_core.py
226 lines (163 loc) · 5.78 KB
/
projectExplorer_core.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
import os
import re
import errno
import json
import shutil
# Import third-party
from source import yaml
import nuke
# from source import lucidity
currentDir = os.path.dirname(__file__).replace("\\", '/')
configPath = currentDir + "/configure.yml"
thumbnailFoldername = '.thumbnails'
thumbnail_ext = 'png'
def read_configFile(file_path):
if not os.path.exists(file_path):
print("config file not exists : %s"%file_path)
templateconfigpath = configPath.replace('.yml', '_template.yml')
if os.path.exists(templateconfigpath) :
shutil.copy2(templateconfigpath, configPath)
else:
return False
with open(file_path, "r") as f :
data = yaml.safe_load(f)
f.close()
return data
def updatePref(data):
with open(configPath, "r") as f :
conf_data = f.read()
f.close()
result = []
for key, value in data.items():
value = value.replace('\\', '/')
for line in conf_data.splitlines():
if key +':' in line :
line = line.split(key+ ":")[0] + "%s: \"%s\""%(key ,value)
result.append(line)
with open(configPath, "w") as f :
f.write('\n'.join(result))
f.close()
class ExplorerCore(object):
def __init__(self):
super(ExplorerCore, self).__init__()
self.configData = read_configFile(configPath)
def getConfigData(self, *args):
data = eval("self.configData" + "".join(["[\"" + key + "\"]" for key in args]))
pattern = re.compile("(?<=\${)([A-Za-z0-9]*)(?=})")
match = pattern.findall(data)
if match:
for key in match :
if key in self.configData['EXT_VAR'].keys():
data = data.replace("${" + key + "}", self.configData['EXT_VAR'][key])
return data
def listProjects(self):
projectPath = self.getConfigData('PATH_TEMPLATE').split("${_PROJECTSNAME}")[0]
nuke.tprint("PROJECTS : %s"%projectPath)
if not os.path.exists(projectPath) :
nuke.tprint("ERROR : Path not found \"%s\""%projectPath)
return []
return [item for item in os.listdir(projectPath) if os.path.isdir("%s/%s"%(projectPath, item))]
def listShots(self,projectname):
shotPath = self.getConfigData('PATH_TEMPLATE').split("${_SHOTSNAME}")[0]
shotPath = shotPath.replace("${_PROJECTSNAME}", projectname)
if not os.path.exists(shotPath) :
nuke.tprint("ERROR : Path not found \"%s\""%shotPath)
return []
result = []
for shot in os.listdir(shotPath):
shotfullpath = "%s/%s"%(shotPath, shot)
if os.path.isdir(shotfullpath) and shot != thumbnailFoldername:
result.append(shot)
subfolder = ["%s/%s"%(shot,sf) for sf in os.listdir(shotfullpath) \
if os.path.isdir("%s/%s"%(shotfullpath, sf)) \
and sf != thumbnailFoldername ]
if subfolder :
result += subfolder
return result
def listVersion(self, project, shot):
savePath = _replaceData(self.getConfigData("PATH_TEMPLATE"), project, shot)
shotDirPath = os.path.dirname(savePath)
return [f for f in os.listdir(shotDirPath) if os.path.isfile(shotDirPath + '/' + f) and f.endswith(('nk', 'nknc'))]
def openScript(self,scriptPath):
nuke.tprint("Openning... \"{}\"".format(scriptPath))
nuke.scriptOpen(scriptPath)
def saveIncrement(self, project, shot):
if not project or not shot :
nuke.tprint("Please select shot")
return False
savePath = _replaceData(self.getConfigData("PATH_TEMPLATE"), project, shot)
filename = os.path.basename(_replaceData(self.getConfigData("SCRIPTNAME_TEMPLATE"), project, shot, get_lastversion(savePath)))
savePath = savePath.replace("${_SCRIPTNAME}", filename)
nuke.scriptSaveAs(savePath)
nuke.tprint("Save file : %s" %(savePath))
return savePath
def get_lastversion(shotPath):
shotDirPath = os.path.dirname(shotPath)
version = []
versionedFiles = [f for f in os.listdir(shotDirPath) if os.path.isfile(shotDirPath + '/' + f) and f.endswith(('nk', 'nknc'))]
if versionedFiles :
for file in versionedFiles :
pattern = re.compile("([vV][0-9]{3}|[vV][0-9]{4})")
match = pattern.findall(file)
if match :
version.append(int(match[0][1:]))
else :
return 1
if not version:
return 1
return max(version) + 1
def _replaceData( data ,project, shot = '', version= ''):
'''
_PROJECTSCODE
_SHOTSNAME
_VERSION
_EXTENSION
'''
_mapping = { "${_PROJECTSNAME}" : project,
"${_PROJECTSCODE}" : "test",
"${_SHOTSNAME}" : shot,
"${_VERSION}" : "%03d"%(version or 0),
"${_EXTENSION}" : "nk" }
for key,value in _mapping.items():
data = data.replace(key, value)
return data
def thumbnailPath(filePath):
thumbnailPath = os.path.dirname(filePath)
name = os.path.splitext(os.path.basename(filePath))[0]
thumbnailPath += '/%s/%s.%s'%(thumbnailFoldername, name, thumbnail_ext)
return thumbnailPath
def saveFrame(thumbnailPath):
viewer = nuke.activeViewer()
inputNode = nuke.ViewerWindow.activeInput(viewer)
viewNode = nuke.activeViewer().node()
filetype = 'png'
try:
if inputNode != None:
selInput = nuke.Node.input(viewNode, inputNode)
if thumbnailPath != None:
if not os.path.exists(os.path.dirname(thumbnailPath)):
os.makedirs( os.path.dirname(thumbnailPath) )
input_w = selInput.width()
input_h = selInput.height()
factor = input_h / 448
w = int(input_w / factor)
h = int(input_h / factor)
reformat= nuke.nodes.Reformat(format="%s %s" % (w, h), resize="width")
reformat.setInput(0,selInput)
write = nuke.nodes.Write(file = thumbnailPath, name = 'WriteSaveThisFrame', file_type=filetype)
write.setInput(0,reformat)
curFrame = int(nuke.knob("frame"))
nuke.execute(write.name(), curFrame, curFrame)
# Clear
nuke.delete(write)
nuke.delete(reformat)
# Succcess
# nuke.tprint("Save thumbnail : " + thumbnailPath)
return thumbnailPath
else:
nuke.message("This viewer don't have any input connected!")
except Exception as e:
nuke.tprint(str(e))
if __name__ == '__main__':
# print listProjects()
print read_configFile("configure.conf")