-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoperators.py
478 lines (402 loc) · 16.5 KB
/
operators.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
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import bpy
import os
import json
import threading
from . import sheepit
import time
import subprocess
def register():
bpy.utils.register_class(SHEEPIT_OT_send_project)
bpy.utils.register_class(SHEEPIT_OT_login)
bpy.utils.register_class(SHEEPIT_OT_logout)
bpy.utils.register_class(SHEEPIT_OT_create_accout)
bpy.utils.register_class(SHEEPIT_OT_refresh_profile)
def unregister():
bpy.utils.unregister_class(SHEEPIT_OT_send_project)
bpy.utils.unregister_class(SHEEPIT_OT_login)
bpy.utils.unregister_class(SHEEPIT_OT_logout)
bpy.utils.unregister_class(SHEEPIT_OT_create_accout)
bpy.utils.unregister_class(SHEEPIT_OT_refresh_profile)
class SHEEPIT_OT_send_project(bpy.types.Operator):
""" Send the current project to the Renderfarm """
bl_idname = "sheepit.send_project"
bl_label = "Send to SheepIt!"
@classmethod
def poll(cls, context):
# test if logged in
preferences = context.preferences.addons[__package__].preferences
if not preferences.logged_in:
return False
# test if renderer is supported
supported_renderers = {'CYCLES', 'BLENDER_EEVEE'}
engine = bpy.context.scene.render.engine
if engine not in supported_renderers:
return False
# Test if at least one device is selected
if engine == 'CYCLES':
if not (context.scene.sheepit_properties.cpu or
context.scene.sheepit_properties.cuda or
context.scene.sheepit_properties.opencl):
return False
else:
if not (context.scene.sheepit_properties.nvidia or
context.scene.sheepit_properties.amd):
return False
# test if allready uploading
if 'sheepit' in bpy.context.window_manager and \
'upload_active' in bpy.context.window_manager['sheepit']:
return not bpy.context.window_manager['sheepit']['upload_active']
return True
def modal(self, context, event):
if event.type == 'TIMER':
# do nothing if thread is still runing
if self.thread.is_alive():
bpy.context.window_manager['sheepit']['progress'] = self.progress
bpy.context.window_manager['sheepit']['upload_status'] = self.status
context.area.tag_redraw()
return {'PASS_THROUGH'}
# test if error occurred
if self.error or self.error_at:
# login error:
if self.error_at == "login" and self.error == "Please Log in":
preferences = context.preferences.addons[__package__].preferences
preferences.logged_in = False
preferences.cookies = ""
preferences.username = ""
self.report({'ERROR'}, f"{self.error_at}: {self.error}")
bpy.context.window_manager['sheepit']['upload_status'] = "Upload failed!"
self.cancel(context)
return {'CANCELLED'}
bpy.context.window_manager['sheepit']['upload_status'] = "Project uploaded!"
self.cancel(context)
return {'FINISHED'}
return {'PASS_THROUGH'}
def execute(self, context):
# prepare cookies
preferences = context.preferences.addons[__package__].preferences
self.cookies = json.loads(preferences.cookies)
# prepare variables
self.animation = context.scene.sheepit_properties.type == 'animation'
self.amd = False
self.nvidia = False
self.cpu = False
if bpy.context.scene.render.engine == 'CYCLES':
self.cpu = context.scene.sheepit_properties.cpu
self.amd = context.scene.sheepit_properties.opencl
self.nvidia = context.scene.sheepit_properties.cuda
else:
self.amd = context.scene.sheepit_properties.amd
self.nvidia = context.scene.sheepit_properties.nvidia
self.public = context.scene.sheepit_properties.public
self.mp4 = context.scene.sheepit_properties.mp4
self.frame_start = context.scene.frame_start
self.frame_end = context.scene.frame_end
self.frame_step = context.scene.frame_step
self.frame_current = context.scene.frame_current
self.split_by_layers = False
if bpy.context.scene.render.engine == 'CYCLES' \
and bpy.context.scene.use_nodes:
self.split_by_layers = True
self.split_tiles = context.scene.sheepit_properties.anim_split
if self.animation:
self.split_layers = context.scene.sheepit_properties.anim_layer_split
else:
self.split_layers = context.scene.sheepit_properties.still_layer_split
# Save file
blend_name = os.path.split(bpy.data.filepath)[1]
if not blend_name:
blend_name = "untitled.blend"
self.filepath = os.path.join(bpy.app.tempdir, blend_name)
bpy.ops.wm.save_as_mainfile(filepath=self.filepath, copy=True)
# Prepare script variables
self.blender_exe = bpy.app.binary_path
self.prepare_script = os.path.join(os.path.dirname(__file__),
"prepare_scene.py"
)
if 'sheepit' not in bpy.context.window_manager:
bpy.context.window_manager['sheepit'] = dict()
bpy.context.window_manager['sheepit']['upload_active'] = True
bpy.context.window_manager['sheepit']['upload_status'] = ""
self.status = ""
bpy.context.window_manager['sheepit']['progress'] = 0
self.progress = 0
self.thread = threading.Thread(target=self.send_project)
self.thread.start()
self.upload_thread = threading.Thread(target=self.update_progress)
self.uploading = True
wm = context.window_manager
self._timer = wm.event_timer_add(0.1, window=context.window)
wm.modal_handler_add(self)
return {'RUNNING_MODAL'}
def send_project(self):
# create error variables
self.error = ""
self.error_at = ""
session = sheepit.Sheepit()
# import cookies
session.import_session(self.cookies)
self.status = "Testing connection"
# Since the new API testing the connection became a more complex
# We will just assume that the user is logged in
self.progress = 5
self.status = "Preparing Scene"
# Prepare scene
r = subprocess.run(
[
self.blender_exe,
self.filepath,
"--background",
"--factory-startup",
"--python",
self.prepare_script
], shell=False
)
try:
with open(f"{self.filepath}.log", "r") as f:
output = f.read().split("<->")
if len(output) == 0 or output[0] != "OK":
if len(output) > 1:
self.error = output[1]
else:
self.error = "unknown error"
self.error_at = "prepare scene"
return
except OSError:
self.error = "Error opening log"
self.error_at = "prepare scene"
return
self.progress = 10
self.status = "Getting Token"
# request a upload token from the SheepIt server
token = ""
try:
token = session.request_upload_token()
except sheepit.NetworkException as e:
self.error = str(e)
self.error_at = "token"
return
except sheepit.UploadException as e:
self.error = str(e)
self.error_at = "token"
return
self.progress = 15
self.status = "Uploading File"
self.token = token
self.upload_thread.start()
# upload the file
try:
session.upload_file(token, self.filepath)
except sheepit.NetworkException as e:
self.error = str(e)
self.error_at = "upload"
self.uploading = False
return
self.uploading = False
if self.upload_thread.is_alive():
self.upload_thread.join()
self.progress = 95
self.status = "Adding Project"
try:
session.add_job(token,
animation=self.animation,
cpu=self.cpu,
cuda=self.nvidia,
opencl=self.amd,
public=self.public,
mp4=self.mp4,
anim_start_frame=self.frame_start,
anim_end_frame=self.frame_end,
anim_step_frame=self.frame_step,
still_frame=self.frame_current,
max_ram="",
split_by_layers=self.split_by_layers,
split_layers=self.split_layers,
split_tiles=self.split_tiles)
except sheepit.NetworkException as e:
self.error = str(e)
self.error_at = "add project"
self.progress = 100
return
def update_progress(self):
session = sheepit.Sheepit()
# import cookies
session.import_session(self.cookies)
while self.uploading:
time.sleep(1)
try:
p = session.get_upload_progress(self.token)
if p:
self.progress = int(15+(p*80))
except Exception:
pass
def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
bpy.context.window_manager['sheepit']['upload_active'] = False
del bpy.context.window_manager['sheepit']['progress']
self.uploading = False
if self.upload_thread.is_alive():
self.upload_thread.join()
if self.thread.is_alive():
self.thread.join()
try:
os.remove(self.filepath)
os.remove(f"{self.filepath}.log")
os.remove(f"{self.filepath}1")
except FileNotFoundError as e:
pass
context.area.tag_redraw()
class SHEEPIT_OT_logout(bpy.types.Operator):
bl_idname = "sheepit.logout"
bl_label = "Logout"
@classmethod
def poll(cls, context):
# test if logged in
preferences = context.preferences.addons[__package__].preferences
return preferences.logged_in
def execute(self, context):
preferences = context.preferences.addons[__package__].preferences
session = sheepit.Sheepit()
# import cookies
session.import_session(json.loads(preferences.cookies))
try:
session.logout()
except sheepit.NetworkException as e:
self.report({'INFO'}, str(e))
# delete preferences
preferences.logged_in = False
preferences.cookies = ""
preferences.username = ""
context.area.tag_redraw()
return {'FINISHED'}
class SHEEPIT_OT_refresh_profile(bpy.types.Operator):
bl_idname = "sheepit.refresh_profile"
bl_label = "Refresh"
@classmethod
def poll(cls, context):
# test if logged in
preferences = context.preferences.addons[__package__].preferences
if not preferences.logged_in:
return False
# test if allready refreshing
if 'sheepit' in bpy.context.window_manager and \
'refresh_active' in bpy.context.window_manager['sheepit']:
return not bpy.context.window_manager['sheepit']['refresh_active']
return True
def modal(self, context, event):
if event.type == 'TIMER':
# do nothing if thread is still runing
if self.thread.is_alive():
return {'PASS_THROUGH'}
# test if error occurred
if type(self.profile) is sheepit.NetworkException:
self.report({'ERROR'}, str(self.profile))
self.cancel(context)
return {'CANCELLED'}
# test if logged in
if not self.profile['Points']:
self.report({'ERROR'}, "Please Log in")
preferences.logged_in = False
preferences.cookies = ""
preferences.username = ""
self.cancel(context)
return {'CANCELLED'}
# save the profile information to the window manager
bpy.context.window_manager['sheepit']['profile'] = self.profile
self.cancel(context)
return {'FINISHED'}
return {'PASS_THROUGH'}
def execute(self, context):
preferences = context.preferences.addons[__package__].preferences
self.cookies = json.loads(preferences.cookies)
self.username = preferences.username
self.thread = threading.Thread(target=self.request_profile)
self.thread.start()
if 'sheepit' not in bpy.context.window_manager:
bpy.context.window_manager['sheepit'] = dict()
if 'profile' not in bpy.context.window_manager['sheepit']:
bpy.context.window_manager['sheepit']['profile'] = dict()
bpy.context.window_manager['sheepit']['refresh_active'] = True
wm = context.window_manager
self._timer = wm.event_timer_add(0.1, window=context.window)
wm.modal_handler_add(self)
return {'RUNNING_MODAL'}
def cancel(self, context):
wm = context.window_manager
wm.event_timer_remove(self._timer)
bpy.context.window_manager['sheepit']['refresh_active'] = False
context.area.tag_redraw()
def request_profile(self):
session = sheepit.Sheepit()
# import cookies
session.import_session(self.cookies)
try:
self.profile = session.get_profile_information(self.username)
except sheepit.NetworkException as e:
self.profile = e
class SHEEPIT_OT_login(bpy.types.Operator):
""" Login to SheepIt! """
bl_idname = "sheepit.login"
bl_label = "Login"
username: bpy.props.StringProperty(name="Username", maxlen=64)
password: bpy.props.StringProperty(
name="Password", subtype='PASSWORD', maxlen=64)
@classmethod
def poll(cls, context):
preferences = context.preferences.addons[__package__].preferences
return not preferences.logged_in
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
def execute(self, context):
# Login with the provided Username and Password
session = sheepit.Sheepit()
error = False
try:
session.login(username=self.username, password=self.password)
except sheepit.NetworkException as e:
self.report({'ERROR'}, str(e))
error = True
except sheepit.LoginException as e:
self.report({'ERROR'}, str(e))
error = True
if error:
# Delete Password
self.password = ""
return {'CANCELLED'}
# Generate preferences
cookies = json.dumps(session.export_session())
# Save
preferences = context.preferences.addons[__package__].preferences
preferences.cookies = cookies
preferences.username = self.username
preferences.logged_in = True
# Delete Password and Username
self.password = ""
self.username = ""
context.area.tag_redraw()
return {'FINISHED'}
class SHEEPIT_OT_create_accout(bpy.types.Operator):
""" Open the Create Account page """
bl_idname = "sheepit.create_account"
bl_label = "Create a SheepIt! Account"
bl_options = {'INTERNAL'}
@classmethod
def poll(cls, context):
return True
def execute(self, context):
bpy.ops.wm.url_open(
url="https://www.sheepit-renderfarm.com/user/register")
return {'FINISHED'}