-
Notifications
You must be signed in to change notification settings - Fork 1
/
__init__.py
608 lines (499 loc) · 19.3 KB
/
__init__.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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
# <pep8-80 compliant>
# ##### BEGIN GPL LICENSE BLOCK #####
#
# 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 2
# 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
# MERCHANTABILITY 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, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ##### END GPL LICENSE BLOCK #####
bl_info = {
"name": "Notify after Render",
"description": "Use internet services to send you notifications at the end of render.",
"author": "Spirou4D",
"version": (1, 2, 0),
"blender": (2, 80, 0),
"location": "Active buttons in properties > render panel",
"warning": "Need the use of \"Auto save render\" add-ons absolutely!",
"wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Render/Notify_after_render",
"tracker_url": "https://github.com/Spirou4D/Notify-after-render/issues",
"category": "Render"
}
import bpy
from bpy.app.handlers import persistent
from bpy.types import Operator, AddonPreferences, Panel
from bpy.props import IntProperty, StringProperty, BoolProperty
from bpy.path import basename
from bpy_extras.io_utils import ExportHelper
# add-on modules import, all setup in this file
# ------------------
import urllib, os, smtplib, email.utils, getpass
# ------------------
import urllib.request
from urllib.parse import urlencode
# ------------------
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.utils import formatdate
# ------------------
from os.path import dirname, exists, join
from os import mkdir, listdir
SEP = os.sep
# -----------------
from re import findall
import pickle
# updater ops import, all setup in this file
from . import addon_updater_ops
def get_addon_preferences():
# bpy.context.preferences.addons["notify_after_render"].preferences['send_sms']=1
# Par exemple:
# addon_prefs = get_addon_preferences()
# addon_prefs.url_smsservice
addon_preferences = bpy.context.preferences.addons[__package__].preferences
return addon_preferences
def get_filepath():
blendname = basename(bpy.data.filepath).rpartition('.')[0]
filepath = dirname(bpy.data.filepath) + SEP + 'auto_saves'
if not exists(filepath):
mkdir(filepath)
if bpy.context.scene.auto_save_subfolders:
filepath = join(filepath, blendname)
if not exists(filepath):
mkdir(filepath)
return filepath
def get_save_ID():
rdff = bpy.context.scene.render.image_settings.file_format
blendname = basename(bpy.data.filepath).rpartition('.')[0]
formats=['BMP', 'IRIS', 'PNG', 'JPEG', 'JPEG2000', 'TARGA', 'TARGA_RAW', \
'CINEON', 'DPX', 'OPEN_EXR_MULTILAYER', 'OPEN_EXR', 'HDR', 'TIFF', \
'AVI_JPEG', 'AVI_RAW', 'FFMPEG']
exts=['.bmp', '.rgb', '.png', '.jpg', '.jp2', '.tga', '.tga', '.cin', \
'.exr', '.exr', '.hdr', '.tiff', '.avi', '.avi', '.ffmpeg']
format = bpy.context.scene.auto_save_format
if format not in formats : format = rdff
i=0
while i < len(formats):
if formats[i]==format : extension = exts[i]
i += 1
filepath = get_filepath()
# imagefiles starting with the blendname
files = [f for f in listdir(filepath) \
if f.startswith(blendname) \
and f.lower().endswith(('.png', '.jpg', '.jpeg', '.exr'))]
highest = 0
if files:
for f in files:
# find last numbers in the filename after the blendname
suffix = findall('\d+', f.split(blendname)[-1])
if suffix:
if int(suffix[-1]) > highest:
highest = int(suffix[-1])
save_ID = blendname + '_' + str(highest).zfill(3) + extension
return save_ID
def NAR_custom_pref_save(context, filepath, WHOLE):
addon_prefs = get_addon_preferences()
if WHOLE == False:
# /home/patrinux/Bureau/notify_after_render.nar
FILEOUTPUT = "%s%s%s.nar" % (filepath.rpartition(SEP)[0], SEP, __name__)
else:
# /home/patrinux/Bureau/notify_after_render_prefs.nar
FILEOUTPUT = "%s%s%s_%s" % (filepath.rpartition(SEP)[0], SEP, __name__, filepath.rpartition(SEP)[-1])
PARAMS = dict()
PARAMS['use_dropbow_service'] = str(addon_prefs.use_dropbow_service)
PARAMS['folderpath_dropbox'] = str(addon_prefs.folderpath_dropbox)
PARAMS['send_mail'] = str(addon_prefs.send_mail)
PARAMS['adress_mail'] = str(addon_prefs.adress_mail)
PARAMS['password_mail'] = str(addon_prefs.password_mail)
PARAMS['smtp_mail'] = str(addon_prefs.smtp_mail)
PARAMS['send_sms'] = str(addon_prefs.send_sms)
PARAMS['url_smsservice'] = str(addon_prefs.url_smsservice)
# Création du fichier .nar
open(FILEOUTPUT, 'wb')
FILE = open(FILEOUTPUT, 'wb')
pickle.dump(PARAMS, FILE)
# Fermeture de l'écriture
FILE.close()
return {'FINISHED'}
def NAR_custom_pref_load(context, filepath, WHOLE):
addon_prefs = get_addon_preferences()
# lecture du fichier .nar
LOADFILE = open(filepath, 'rb')
PYRAW = pickle.load(LOADFILE)
if len(PYRAW) == 8:
if PYRAW["use_dropbow_service"] == "True":
addon_prefs.use_dropbow_service = True
else:
addon_prefs.use_dropbow_service = False
addon_prefs.folderpath_dropbox = PYRAW["folderpath_dropbox"]
if PYRAW["send_mail"] == "True":
addon_prefs.send_mail = True
else:
addon_prefs.send_mail = False
addon_prefs.adress_mail = PYRAW["adress_mail"]
addon_prefs.password_mail = PYRAW["password_mail"]
addon_prefs.smtp_mail = PYRAW["smtp_mail"]
if PYRAW["send_sms"] == "True":
addon_prefs.send_sms = True
else:
addon_prefs.send_sms = False
addon_prefs.url_smsservice = PYRAW["url_smsservice"]
else:
print("This file isn\'t an official .nar pref file.")
return {'CANCELLED'}
# Fermeture de la lecture
LOADFILE.close()
return {'FINISHED'}
def notifications_UI(self, context):
addon_prefs = get_addon_preferences()
layout = self.layout
split = layout.split(factor=0.33, align=True)
row = split.row()
row.label(text="Notification by:")
row = split.row(align=True)
row.prop(addon_prefs, 'send_sms', text='SMS', toggle=False)
row.prop(addon_prefs, 'send_mail', text='Mail', toggle=False)
row = split.row()
row.prop(addon_prefs, 'use_dropbow_service', text='Use Dropbox', toggle=False)
@persistent
def notifications_handler(scene):
addon_prefs = get_addon_preferences()
# SMS
A1 = addon_prefs.send_sms
myRender = get_save_ID()
smsText = 'At :' + formatdate(localtime=True) + \
', your render [ ' + str(myRender) + ' ] is ready!'
URL = addon_prefs.url_smsservice
URL = URL + '&msg=' + urllib.parse.quote_plus(smsText)
# MAIL
A2 = addon_prefs.send_mail
# DROPBOX
A3 = addon_prefs.use_dropbow_service
Dpath = addon_prefs.folderpath_dropbox
# ==========================================
if A1 and URL == '':
print('You must setup the url of your sms service, please!')
elif A1 and not URL == '':
smsx = urllib.request.urlopen(URL, data=None)
else:
print('No SMS sent!')
if A2:
bpy.ops.render.notify_sendmail()
else:
print('No mail sent!')
if A3 and Dpath == '':
print('You must choose a dropbox folder in user preferences, please!')
elif A3 and not Dpath == '':
bpy.ops.render.copy_render_dropbox()
else:
print('No render copied in dropbox!')
class EXPORT_OT_preferences_save(Operator, ExportHelper):
'''Only in the user prefs panel'''
bl_idname = "export.preferences_export"
bl_label = "Save"
bl_description = "Save custom preferences in text file."
filename_ext = ".nar"
filter_glob : StringProperty(
default="*.nar",
options={'HIDDEN'},
)
use_setting : BoolProperty(
name="With specification",
description="Name automatically \"notify_after_render.nar\".",
default=True,
)
def execute(self, context):
return NAR_custom_pref_save(context, self.filepath, self.use_setting)
class IMPORT_OT_preferences_load(Operator, ExportHelper):
'''Only in the user prefs panel'''
bl_idname = "import.preferences_load"
bl_label = "Load"
bl_description = "Load custom preference from text file."
filename_ext = ".nar"
filter_glob : StringProperty(
default="*.nar",
options={'HIDDEN'},
)
use_setting : BoolProperty(
name="With specification",
description="Name automatically \"notify_after_render.nar\".",
default=True,
)
def execute(self, context):
return NAR_custom_pref_load(context, self.filepath, self.use_setting)
class RENDER_OT_copy_render_dropbox(Operator):
"""Copy render in you Dropbox"""
# bpy.ops.render.copy_render_dropbox()
bl_idname = "render.copy_render_dropbox"
bl_label = "Copy still render dropbox"
bl_description = "Copy still render in your favorite Dropbox's folder."
def execute(self, context):
scene = context.scene
addon_prefs = get_addon_preferences()
A1 = addon_prefs.use_dropbow_service
A2 = scene.auto_save_after_render
A3 = addon_prefs.folderpath_dropbox
if not A1 or not A2 or A3 == '' or bpy.data.filepath =='':
return
rndr = scene.render
original_format = rndr.image_settings.file_format
save_ID = get_save_ID()
copied_name = join(A3, save_ID)
image = bpy.data.images['Render Result']
if not image:
print('Dropbox_Save: Render Result not found. Image not copied')
return
try:
image.save_render(copied_name, scene=None)
print('Dropbox_Save:', copied_name)
except:
pass
rndr.image_settings.file_format = original_format
return {'FINISHED'}
def invoke(self, context, event):
try:
execute(scene)
print("Successfully render copied!")
except:
print("Error: unable to copy the render!")
return {'FINISHED'}
class RENDER_OT_notify_sendmail(Operator):
"""Send Mail"""
# bpy.ops.render.notify_sendmail()
bl_idname = "render.notify_sendmail"
bl_label = "Send mail"
bl_description = "Send a mail to notify you after a render."
def execute(self, context):
addon_prefs = get_addon_preferences()
mFrom = addon_prefs.adress_mail
passW = addon_prefs.password_mail
smtpSer = addon_prefs.smtp_mail
myRender = get_save_ID()
if mFrom == '[email protected]' or smtpSer == 'smtp.blender.org':
print('You must clearly setup your mail informations, please!')
return
# Message contenant du text/plain
Text = 'At :' + formatdate(localtime=True) + \
', your render [ ' + str(myRender) + ' ] is ready!'
# Entêtes : from/to/subject
msg = MIMEMultipart()
msg['From'] = email.utils.formataddr(('Author', mFrom))
msg['To'] = email.utils.formataddr(('Recipient', mFrom))
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = "Blender has finished..."
msg.attach(MIMEText(Text))
# création d'un objet 'serveur'
myserver = smtplib.SMTP(smtpSer, 587) # raw 25 or SSL/TLS 465 or raw 587
try:
myserver.set_debuglevel(True)
# identify ourselves, prompting server for supported features
myserver.ehlo()
# If we can encrypt this session, do it
if myserver.has_extn('STARTTLS'):
myserver.starttls()
myserver.ehlo() # re-identify ourselves over TLS connection
myserver.login(mFrom, passW)
# Envoie du mail
myserver.sendmail(mFrom, [mFrom], msg.as_string())
finally:
myserver.quit()
return {'FINISHED'}
def invoke(self, context, event):
try:
execute(context)
print("Successfully sent email")
except Exception as err:
print('Unable to send email: Not found -> ', err)
return {'FINISHED'}
class OBJECT_PT_UpdaterPanel(Panel):
"""Panel to demo popup notice and ignoring functionality"""
bl_label = "Notify after render"
bl_idname = "OBJECT_PT_UpdaterPanel"
bl_space_type = 'VIEW_3D'
bl_region_type = 'UI'
bl_context = "objectmode"
bl_category = "Tools"
bl_options = {'DEFAULT_CLOSED'}
def draw(self, context):
layout = self.layout
# Call to check for update in background
# note: built-in checks ensure it runs at most once
# and will run in the background thread, not blocking
# or hanging blender
# Internally also checks to see if auto-check enabled
# and if the time interval has passed
addon_updater_ops.check_for_update_background()
layout.label(text="Notify after render Update")
layout.label(text="")
col = layout.column()
col.scale_y = 0.7
col.label(text="If an update is ready,")
col.label(text="popup triggered by opening")
col.label(text="this panel, plus a box ui")
# could also use your own custom drawing
# based on shared variables
if addon_updater_ops.updater.update_ready == True:
layout.label(text="Custom update message", icon="INFO")
layout.label(text="")
# call built-in function with draw code/checks
addon_updater_ops.update_notice_box_ui(self, context)
# add-on preferences
class NAR_Preferences(AddonPreferences):
bl_idname = __package__
# addon updater preferences
auto_check_update : BoolProperty(
name="Auto-check for Update",
description="If enabled, auto-check for updates using an interval",
default=False,
)
updater_intrval_months : IntProperty(
name='Months',
description="Number of months between checking for updates",
default=0,
min=0
)
updater_intrval_days : IntProperty(
name='Days',
description="Number of days between checking for updates",
default=7,
min=0,
max=31
)
updater_intrval_hours : IntProperty(
name='Hours',
description="Number of hours between checking for updates",
default=0,
min=0,
max=23
)
updater_intrval_minutes : IntProperty(
name='Minutes',
description="Number of minutes between checking for updates",
default=0,
min=0,
max=59
)
# ---------------------DROPBOX
use_dropbow_service : BoolProperty(
name="Use Dropbox",
description="If you want to use a dropbox service:",
default=False,
)
folderpath_dropbox : StringProperty(
description="Path to your dropbox 's folder:",
name="Dropbox folder path",
subtype='DIR_PATH',
)
# ----------------------SMS
send_sms : BoolProperty(
name="Send SMS",
description="If you want to send an informative sms:",
default=False,
)
url_smsservice : StringProperty(
description="Preference of sms service",
name="SMS sevice URL",
subtype='PASSWORD',
)
# ----------------------MAIL
send_mail : BoolProperty(
name="Send Mail",
description="If you want to send an informative mail:",
default=False,
)
adress_mail : StringProperty(
description="Test of the mail",
name="Mail",
default="[email protected]",
)
password_mail : StringProperty(
description="Test of the mail",
name="Password",
subtype='PASSWORD',
)
smtp_mail : StringProperty(
description="Test of the mail",
name="SMTP server",
default="smtp.blender.org",
)
def draw(self, context):
layout = self.layout
mainrow = layout.row()
col = mainrow.column()
split = col.split(factor=0.20)
row = split.row()
row.prop(self, "send_sms")
if self.send_sms == True:
row = split.row()
row.prop(self, "url_smsservice")
split = col.split(factor=0.20)
row = col.row()
row.prop(self, "send_mail")
if self.send_mail == True:
row = col.row()
row.prop(self, "adress_mail")
row.prop(self, "password_mail")
row.prop(self, "smtp_mail")
split = col.split(factor=0.20)
row = split.row()
row.prop(self, "use_dropbow_service")
if self.use_dropbow_service == True:
row = split.row()
row.prop(self, "folderpath_dropbox")
box = col.box()
row = box.row()
row.label(text="Custom preferences :")
row.operator('import.preferences_load', text='Load')
row.operator('export.preferences_export', text='Save')
# -----------------------------------------------------------------UPDATER
# col = layout.column() # works best if a column, or even just self.layout
col = mainrow.column()
# updater draw function
# could also pass in col as third arg
addon_updater_ops.update_settings_ui(self, context)
# Alternate draw function, which is more condensed and can be
# placed within an existing draw function. Only contains:
# 1) check for update/update now buttons
# 2) toggle for auto-check (interval will be equal to what is set above)
# addon_updater_ops.update_settings_ui_condensed(self, context, col)
# Adding another column to help show the above condensed ui as one column
# col = mainrow.column()
# col.scale_y = 2
# col.operator("wm.url_open","Open webpage ").url=addon_updater_ops.updater.website
classes = (
EXPORT_OT_preferences_save,
IMPORT_OT_preferences_load,
RENDER_OT_copy_render_dropbox,
RENDER_OT_notify_sendmail,
OBJECT_PT_UpdaterPanel,
NAR_Preferences
)
def register():
# addon updater code and configurations
# in case of broken version, try to register the updater first
# so that users can revert back to a working version
addon_updater_ops.register(bl_info)
# register classes
for cls in classes:
addon_updater_ops.make_annotations(OBJECT_PT_UpdaterPanel) # to avoid blender 2.8 warnings
bpy.utils.register_class(cls)
bpy.types.RENDER_PT_context.append(notifications_UI)
bpy.app.handlers.render_post.append(notifications_handler)
def unregister():
bpy.types.RENDER_PT_context.remove(notifications_UI)
bpy.app.handlers.render_post.remove(notifications_handler)
# addon updater unregister
addon_updater_ops.unregister()
# register classes
for cls in reversed(classes):
bpy.utils.unregister_class(cls)
if __name__ == "__main__":
register()