-
Notifications
You must be signed in to change notification settings - Fork 0
/
NYOR_Fundamentals.py
228 lines (157 loc) · 6.44 KB
/
NYOR_Fundamentals.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
#! python3
from pathlib import Path, PurePath
from itertools import groupby
from send2trash import send2trash
# -------------------------------------------------------------------------------
# Utilities
def get_tag_value(file_name, tag_name, original_string=False):
render_tag = NS_render_settings.tags[tag_name]
start = file_name.find(render_tag["start_marker"])
if start == -1:
return {"error": "start_marker_not_found"}
start += len(render_tag["start_marker"])
end = file_name.find(render_tag["end_marker"], start)
if end == -1:
return {"error": "end_marker_not_found"}
value = file_name[start:end]
if not original_string:
return {"value": int(value)}
return {"value": value}
# Wrapper functions, for easier-to-understand usage
def get_comp_number(file_name, original_string=False):
comp_number = get_tag_value(file_name, "comp_number", original_string)
if "value" in comp_number:
return comp_number["value"]
if "error" in comp_number:
if comp_number["error"] == "end_marker_not_found":
return 1
if comp_number["error"] == "start_marker_not_found":
raise Exception("get_comp_number Error: start_marker not found.")
else:
raise Exception("get_comp_number Error: No keys found.")
def get_version_number(file_name, original_string=False):
version_number = get_tag_value(file_name, "version_number", original_string)
if "value" in version_number:
return version_number["value"]
if "error" in version_number:
if version_number["error"] == "end_marker_not_found":
raise Exception("get_version_number Error: end_marker not found.")
if version_number["error"] == "start_marker_not_found":
raise Exception("get_version_number Error: start_marker not found.")
else:
raise Exception("get_version_number Error: No keys found.")
def get_frame_number(file_name, original_string=False):
frame_number = get_tag_value(file_name, "frame_number", original_string)
if "value" in frame_number:
return frame_number["value"]
if "error" in frame_number:
if frame_number["error"] == "end_marker_not_found":
raise Exception("get_frame_number Error: end_marker not found.")
if frame_number["error"] == "start_marker_not_found":
raise Exception("get_frame_number Error: start_marker not found.")
else:
raise Exception("get_frame_number Error: No keys found.")
# -------------------------------------------------------------------------------
def get_comps_files(shot):
comps_files = list(shot.comps_path.glob(f"{shot.name}_comp_*.nk"))
return comps_files
def get_render_files(shot, extension="dpx"):
'''
Arguments: shot, extension
Valid "extension" values(default: "dpx"):
"<file_ext>"
"all"
'''
if (extension not in NS_render_settings.extensions) and (extension != "all"):
raise Exception(f"get_render_files Error: Unknown file extension '{extension}'.")
if extension == "all":
render_files = []
for extension in NS_render_settings.extensions:
render_files.append(list(shot.renders_path.glob(f"{shot.name}_comp_*.{extension}")))
return render_files
render_files = list(shot.renders_path.glob(f"{shot.name}_comp_*.{extension}"))
return render_files
def get_tmp_files(project):
tmp_files = list(project.path.glob(f"*/{project.renders_path}/*_comp_*.tmp"))
return tmp_files
# -------------------------------------------------------------------------------
def filter_by(element, filter_type):
'''
Possible filter types: "comp"; "version"
"comp": should be applied to an 'element' that is a regular list of paths.
"version": should be applied to an 'element' that is a regular list of paths.
'''
if filter_type == "comp":
comps = []
for comp_number, comp_files in groupby(element, lambda file: get_comp_number(file.name)):
comp_files = sorted(comp_files, key=lambda file: get_comp_number(file.name))
comps.append((comp_number, comp_files))
return comps
if filter_type == "version":
versions = []
for version_number, version_files in groupby(element[1], lambda file: get_version_number(file.name)):
version_files = sorted(version_files, key=lambda file: get_version_number(file.name))
versions.append((version_number, version_files))
return versions
else:
raise Exception(f"filter_by Error: Invalid filter type '{filter_type}'.")
# -------------------------------------------------------------------------------
# Models
class RenderSettings:
def __init__(self, name):
self.name = name
self.tags = {}
self.extensions = []
def add_tag(self, name, start_marker, end_marker):
if name in self.tags:
raise Exception(f"RenderSettings.add_tag Error: Tag '{name}' is already in the settings.")
self.tags[name] = {"start_marker": start_marker, "end_marker": end_marker}
def add_extension(self, extension):
if extension in self.extensions:
raise Exception(f"RenderSettings.add_extension Error: Extension '{extension}' is already in the settings.")
self.extensions.append(extension)
def extensions_list(self):
pass
def __str__(self):
return f"RenderSettings: {self.name}"
# class File:
# def __init__(self, path):
# self.path = Path(path)
# self.name = self.path.name
# def __str__(self):
# return f"File: {self.name}"
# class Frame:
# def __init__(self, file):
# self.file = file
# self.string = get_tag_value(self.file.name, "frame_number", original_string=True)
# self.number = get_tag_value(self.file.name, "frame_number")
# def __str__(self):
# return str(f"Frame {self.string}")
class Render:
def __init__(self, frames, extension="dpx"):
self.frames = frames # [sorted_list_of_frames]
self.extension = extension
def delete(self, send_to_trash=True):
for frame in self.frames:
# print(f"Deleting frame. {str(frame.resolve())}")
if not send_to_trash:
frame.unlink()
send2trash(str(frame.resolve()))
tmp_file = list(frame.parent.glob(f"{frame.name}*.tmp"))
if tmp_file == []:
continue
tmp_file = tmp_file[0]
if not send_to_trash:
tmp_file.unlink()
print(f"Deleting tmp_file: {str(tmp_file.name)}")
send2trash(str(tmp_file.resolve()))
# print("Frame Deleted.")
def __str__(self):
return f"Render ({len(self.frames)} frames)"
# -------------------------------------------------------------------------------
# NukeStudio RenderSettings setup
NS_render_settings = RenderSettings("NS_render_settings")
NS_render_settings.add_extension("dpx")
NS_render_settings.add_tag("comp_number", "_comp_", "_v")
NS_render_settings.add_tag("version_number", "_v", ".")
NS_render_settings.add_tag("frame_number", ".", ".")