-
Notifications
You must be signed in to change notification settings - Fork 1
/
pyburner.py
486 lines (427 loc) · 17.4 KB
/
pyburner.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
from __future__ import print_function
from csv_parser import get_job_name, return_frames, servers_sorted
import os
import sys
import csv
import socket
import platform
import subprocess
import tkPopup
try:
# python 3
from tkinter import filedialog, messagebox
import tkinter as tk
import configparser
from tkinter.font import Font, nametofont
except ImportError:
# python 2
import Tkinter as tk
from tkFont import Font, nametofont
import tkMessageBox as messagebox
import tkFileDialog as filedialog
import ConfigParser as configparser
try:
FileNotFoundError
except NameError:
# for Python2
FileNotFoundError = IOError
os_name = platform.system()
BGCOLOR = '#222222'
FGCOLOR = '#f0f0f0'
def truncate_file(file):
try:
with open(file, 'r+') as f:
f.truncate()
except FileNotFoundError:
pass
def add_quotes(txt):
return '"{}"'.format(txt)
def config_reader(section):
cfg = configparser.RawConfigParser()
cfg.read('config.ini')
config_dict = {}
options = cfg.options(section)
for option in options:
try:
config_dict[option] = cfg.get(section, option)
if config_dict[option] == -1:
print("skip: %s" % option)
except Exception:
print("exception on %s!" % option)
config_dict[option] = None
return config_dict
def config_writer(section, option, value):
config = configparser.ConfigParser()
config.read('config.ini')
config.set(section, option, value)
with open('config.ini', 'w') as cfgfile:
config.write(cfgfile)
def test_network(r_manager):
try:
ip_ = socket.gethostbyname(r_manager)
return ip_
except socket.gaierror:
err_message = '''Check your network connection.\nIs your render server available?'''
messagebox.showerror('Network Error', err_message)
return 0
class MyTextSettings(tk.Text):
def set_text(self, text):
self.insert(tk.END, text + '\n', 'txt_indent')
self.see(tk.END)
if os_name == 'Darwin':
my_font = Font(family="Lucida Console", size=11)
elif os_name == 'Windows':
my_font = Font(family="Consolas", size=9)
self.configure(
font=my_font, bg='#333333', wrap=tk.WORD, fg=FGCOLOR, highlightthickness=0
)
def clear_all(self):
self.delete("1.0", tk.END)
def clear_help(self):
self.clear_all()
self.set_text('Use "File --> Open" to choose .csv or .txt file')
class MyLabel(tk.Label):
def __init__(self, *args, **kwargs):
tk.Label.__init__(self, *args, **kwargs)
self.configure(bg=BGCOLOR, fg=FGCOLOR, highlightthickness=0)
class MainApplication(tk.Tk):
"""main application window
Attributes:
all_servers (list): returns list of all servers
customFont (TYPE): defined two types of fonts for mac/win compatibility
entry (entry field): enter number of the server that failed to render
file_contents (list): the contents of the Backburner job file
ip_address (txt): check if the render manager is available and fill it's ip address
job_name (txt): Backburner job name
L1 (UI label): label
PRIORITY (txt): set priority for new Backburner job
RENDER_MANAGER (txt): render manager name
run_button (button): run program to create .bat-render file
SCENE_LOOKUP (txt): path to the folder with .max files
selected_server (txt): failed server name you have chosen
server_frames_list (list): list of frames assigned to failed server to be re-rendered
servers (list): list of servers in Backburner job
text (txt): console text field
the_csv_file (file): Backburner job exported file
var (int): 1 - to call 'open result' function; 0 - to pass
VERSION (txt): 3DsMax version
"""
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
self.geometry('+300+100')
self.title('pyBurner')
self.configure(bg=BGCOLOR)
if os_name == 'Darwin':
self.customFont = Font(family="Lucida Console", size=10)
txt_area = [18, 53]
elif os_name == 'Windows':
txt_area = [18, 48]
self.customFont = nametofont("TkDefaultFont")
self.customFont.configure(size=9)
# text area
frame1 = tk.Frame(self)
frame1.configure(background=BGCOLOR)
frame1.grid(row=0, column=0, columnspan=2, sticky='w')
scrollbar = tk.Scrollbar(frame1)
self.text = MyTextSettings(
frame1, height=txt_area[0], width=txt_area[1], yscrollcommand=scrollbar.set
)
self.text.pack(padx=(4, 0), side=tk.LEFT, expand=True)
scrollbar.configure(command=self.text.yview)
scrollbar.pack(side=tk.LEFT, fill=tk.Y, expand=False)
# labels area
l_frame = tk.Frame(self)
l_frame.config(background=BGCOLOR)
l_frame.grid(row=1, column=0, pady=5)
self.L1 = MyLabel(l_frame, font=self.customFont)
self.L1.pack()
self.var = tk.IntVar()
checkbutton1 = tk.Checkbutton(
l_frame,
font=self.customFont,
background=BGCOLOR,
activebackground=BGCOLOR,
variable=self.var,
)
checkbutton1.pack(side=tk.LEFT)
L2 = MyLabel(l_frame, bg="red", text="open result", font=self.customFont)
L2.pack(side=tk.LEFT)
# buttons area
b_frame = tk.Frame(self)
b_frame.config(bg=BGCOLOR)
b_frame.grid(row=1, column=1, padx=(0, 20), sticky='e')
submit_button = tk.Button(
b_frame, text='submit', command=self.get_server_entry, font=self.customFont
)
submit_button.grid(row=1, column=3, padx=(0, 3), sticky='w')
submit_button.configure(highlightbackground=BGCOLOR)
self.bind('<Return>', self.get_server_entry)
self.run_button = tk.Button(
b_frame, text='run', command=self.run_app, font=self.customFont
)
self.run_button.configure(highlightbackground=BGCOLOR)
self.run_button.grid(row=1, column=4, padx=(0, 3))
self.bind('<Control-r>', self.run_app)
reset_button = tk.Button(
b_frame, text='reset', command=self.cleanup, font=self.customFont
)
reset_button.config(highlightbackground=BGCOLOR)
reset_button.grid(row=1, column=5, sticky='we')
self.showall_button = tk.Button(
b_frame, text='all jobs', command=self.show_all, state=tk.DISABLED
)
self.showall_button.configure(highlightbackground=BGCOLOR, font=self.customFont)
self.showall_button.grid(row=2, column=3, sticky='w')
close_button = tk.Button(
b_frame, text='close', command=self.quit_app, font=self.customFont
)
close_button.configure(highlightbackground=BGCOLOR)
close_button.grid(row=2, column=4, columnspan=2, sticky='we')
self.entry = tk.Entry(b_frame, width=6, font=self.customFont)
self.entry.grid(row=1, column=2, sticky='w', padx=(0, 6))
self.entry.configure(
background="#535353", foreground=FGCOLOR, highlightthickness=0
)
# file menu
menubar = tk.Menu(self)
filemenu = tk.Menu(menubar, tearoff=0)
self.config(menu=menubar)
filemenu.add_command(label='Open', command=self.csv_open, accelerator="Ctrl+O")
filemenu.add_command(label='Preferences', command=self.open_preferences)
filemenu.add_separator()
filemenu.add_command(label='Exit', command=self.quit_app, accelerator='Ctrl+Q')
self.bind("<Control-o>", self.csv_open)
self.bind("<Control-q>", self.quit_app)
menubar.add_cascade(label='File', menu=filemenu)
self.text.clear_help()
self.load_defaults()
def cleanup(self):
self.text.clear_help()
self.load_defaults()
def load_defaults(self):
self.var.set(1)
self.L1.config(text='press CTRL+O to open file')
self.entry.delete("0", tk.END)
self.job_name = None
self.selected_server = None
self.file_contents = []
self.servers = []
self.all_servers = []
self.the_csv_file = None
self.read_config()
self.showall_button.config(state=tk.DISABLED)
def read_config(self):
try:
self.SCENE_LOOKUP = config_reader('settings')['path']
self.PRIORITY = config_reader('settings')['priority']
self.RENDER_MANAGER = config_reader('settings')['manager']
self.VERSION = config_reader('settings')['version']
except (configparser.NoSectionError, KeyError):
sample_config = r'''[settings]
priority = 100
path = ~\Documents
version = 2016
manager = localhost'''
with open('config.ini', 'w') as cfgfile:
cfgfile.write(sample_config)
def show_all(self):
if self.the_csv_file:
ShowAllWindow('all frames', file=self.the_csv_file)
else:
self.text.set_text('open file first')
@staticmethod
def open_preferences():
OpenPrefs('Preferences')
def csv_open(self, *args):
self.text.clear_all()
self.the_csv_file = filedialog.askopenfilename(
initialdir='{}/Desktop'.format(os.path.expanduser('~')),
filetypes=(
('Text File', '*.txt'), ('CSV file', '*.csv'), ('All Files', '*.*')
),
title='Choose a file',
)
if self.the_csv_file:
try:
self.job_name = get_job_name(self.the_csv_file)
if self.job_name is None:
self.text.set_text('Job name is empty, check file')
return
else:
self.showall_button.config(state=tk.NORMAL)
except (csv.Error, UnicodeDecodeError):
self.text.set_text('This file is not valid, try another!')
return
self.all_servers = servers_sorted(self.the_csv_file)
self.text.set_text('Job name: {}\n'.format(self.job_name))
self.text.set_text(
"Found {} servers in file:".format(len(self.all_servers))
)
for num, serv in enumerate(self.all_servers):
self.text.set_text('{}) {}'.format(num + 1, serv))
self.text.set_text('\nenter server number and submit (hit ENTER)')
self.L1.config(
text='Enter server number (1-{})'.format(len(self.all_servers))
)
self.entry.delete("0", tk.END)
self.entry.focus()
else:
self.text.clear_help()
def get_server_entry(self, event=None):
try:
server_num = int(self.entry.get().strip())
if server_num > 0:
self.server_frames_list = list(
return_frames(
self.the_csv_file, self.all_servers[int(server_num) - 1]
)
)
self.selected_server = self.all_servers[int(server_num) - 1]
self.text.set_text('\nyou\'ve selected server #{}'.format(server_num))
self.text.set_text("'{}'".format(self.selected_server))
self.text.set_text(
'Now press RUN button (or hit "Space")\nand choose .max file'
)
self.run_button.focus()
else:
self.text.set_text('enter number greater than zero')
except (ValueError, IndexError):
self.text.set_text('Enter correct number!')
def choose_max_file(self):
open_maxfile = filedialog.askopenfilename(
initialdir=self.SCENE_LOOKUP,
filetypes=(('3dMax file', '*.max'), ('All Files', '*.*')),
title='Choose MAX file',
)
if open_maxfile:
return open_maxfile
else:
self.text.set_text('\nClick "run" and choose .max file!')
return 0
def run_app(self, event=None):
if self.job_name and self.selected_server:
self.read_config()
max_file = self.choose_max_file()
if max_file:
self.text.set_text('\nThese frames will be rendered again:')
self.text.set_text(", ".join(self.server_frames_list))
self.text.set_text('\r')
self.ip_address = test_network(
self.RENDER_MANAGER
) # test network path again just in case
if self.ip_address:
norm_path = os.path.normpath(max_file)
self.make_bat(norm_path)
else:
self.text.set_text(
'\nCheck server settings in Preferences and run again'
)
elif not self.job_name:
self.text.set_text("You should select jobs file first")
elif not self.selected_server:
self.text.set_text("Enter server number and submit!")
@staticmethod
def open_result(folder):
# show the result folder in Windows Explorer or macOS Finder
if os_name == 'Darwin':
subprocess.Popen(['open', folder])
elif os_name == 'Windows':
subprocess.Popen('explorer /open, {}'.format(folder))
else:
pass
def make_bat(self, max_path):
max_version = '\"C:\\Program Files\\Autodesk\\3ds Max {}\\3dsmaxcmd.exe\"'.format(
self.VERSION
)
quoted_max_file = add_quotes(max_path)
max_folder, max_file = os.path.split(max_path)
filename, _ = os.path.splitext(max_file)
bat_file = os.path.join(
max_folder, '{}_{}.bat'.format(filename, self.selected_server)
)
truncate_file(bat_file)
with open(bat_file, 'a') as bat:
print(max_version, quoted_max_file, file=bat, end=' ')
print(
'-frames:{}'.format(",".join(self.server_frames_list)),
file=bat,
end=' ',
)
# print(",".join(self.server_frames_list), file=bat, end=' ')
print('-submit:', self.ip_address, file=bat, end=' ')
print(
'-jobname: {}_{}'.format(self.job_name, self.selected_server),
file=bat,
end=' ',
)
print('-priority:{}'.format(self.PRIORITY), file=bat)
if self.var.get() == 1:
self.text.set_text('Opening folder...\n')
self.open_result(max_folder)
self.var.set(0) # uncheck button to prevent multiple windows when re-run
else:
pass
self.text.set_text(
'Done!\nCheck "{}" at {}'.format(os.path.split(bat_file)[1], max_folder)
)
self.entry.focus()
@staticmethod
def quit_app(event=None):
sys.exit(0)
class OpenPrefs(tkPopup.PopupWindow):
def body(self, master):
self.geometry('+680+100')
self.config(bg=BGCOLOR, takefocus=True, padx=20, pady=5)
window_frame = tk.Frame(self)
enter_label = MyLabel(window_frame, text='render manager: ')
enter_label.grid(row=0, column=0, sticky='w')
self.ip_label = MyLabel(window_frame, text='')
self.ip_label.grid(column=1, sticky='w')
self.serv_entry = tk.Entry(window_frame)
self.serv_entry.configure(bg='#535353', fg=FGCOLOR, width=15)
self.serv_entry.grid(row=0, column=1, sticky='we')
self.manager = config_reader('settings')['manager']
test_button = tk.Button(window_frame, text='test', command=self.validate)
test_button.configure(highlightbackground=BGCOLOR)
test_button.grid(row=0, column=2, sticky='we', padx=(5, 0))
reset_button = tk.Button(window_frame, text='reset', command=self.set_local)
reset_button.grid(row=0, column=3, sticky='we')
reset_button.configure(highlightbackground=BGCOLOR)
self.serv_entry.insert(tk.END, self.manager)
window_frame.config(bg=BGCOLOR)
window_frame.pack()
self.buttonbox(BGCOLOR)
def set_local(self):
self.serv_entry.delete(0, tk.END)
self.serv_entry.insert(tk.END, 'localhost')
def validate(self):
self.new_manager = self.serv_entry.get()
ipaddress = test_network(self.new_manager)
if ipaddress:
self.ip_label.config(text=ipaddress)
return 1
else:
self.ip_label.config(text='connection error')
return 0
def apply(self):
config_writer('settings', 'manager', self.new_manager)
class ShowAllWindow(tkPopup.PopupWindow):
def body(self, master, file):
self.geometry('+680+100')
self.resizable(0, 0)
s_frame = tk.Frame(self, bg=BGCOLOR, highlightthickness=0)
s_frame.pack()
sb = tk.Scrollbar(s_frame)
txt_all = MyTextSettings(s_frame, width=42, yscrollcommand=sb.set)
txt_all.pack(side=tk.LEFT, padx=(5, 0))
sb.configure(highlightcolor=BGCOLOR, command=txt_all.yview)
sb.pack(side=tk.LEFT, fill=tk.Y, expand=False)
for item in servers_sorted(file):
txt_all.set_text(item)
txt_all.set_text(', '.join(return_frames(file, item)))
txt_all.set_text('\r')
self.buttonbox(BGCOLOR, pad=20)
if __name__ == '__main__':
app = MainApplication()
app.resizable(width=tk.FALSE, height=tk.FALSE)
app.mainloop()