added links
[blender-addons-contrib.git] / render_auto_save.py
1 bl_info = {
2     "name": "Auto Save Render",
3     "author": "tstscr",
4     "version": (1, 0),
5     "blender": (2, 6, 3),
6     "location": "Rendertab -> Render Panel",
7     "description": "Automatically save the image after rendering",
8     "warning": "",
9     "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/Render/Auto_Save",
10     "tracker_url": "http://projects.blender.org/tracker/index.php?func=detail&aid=32491",
11     "category": "Render"}
12 ###########################################################################
13 import bpy
14 from bpy.props import BoolProperty, EnumProperty
15 from bpy.app.handlers import persistent
16 from os.path import dirname, exists, join
17 from bpy.path import basename
18 from os import mkdir, listdir
19 from re import findall
20
21
22 @persistent
23 def auto_save_render(scene):
24     if not scene.save_after_render or not bpy.data.filepath:
25         return
26     rndr = scene.render
27     original_format = rndr.image_settings.file_format
28
29     format = rndr.image_settings.file_format = scene.auto_save_format
30     if format == 'OPEN_EXR_MULTILAYER': extension = '.exr'
31     if format == 'JPEG': extension = '.jpg'
32     if format == 'PNG': extension = '.png'
33     
34     blendname = basename(bpy.data.filepath).rpartition('.')[0]
35     filepath = dirname(bpy.data.filepath) + '/auto_saves'
36     
37     if not exists(filepath):
38         mkdir(filepath)
39         
40     if scene.auto_save_subfolders:
41         filepath = join(filepath, blendname)
42         if not exists(filepath):
43             mkdir(filepath)
44
45     #imagefiles starting with the blendname
46     files = [f for f in listdir(filepath) \
47             if f.startswith(blendname) \
48             and f.lower().endswith(('.png', '.jpg', '.jpeg', '.exr'))]
49     
50     highest = 0
51     if files:
52         for f in files:
53             #find last numbers in the filename after the blendname
54             suffix = findall('\d+', f.split(blendname)[-1])
55             if suffix:
56                 if int(suffix[-1]) > highest:
57                     highest = int(suffix[-1])
58     
59     save_name = join(filepath, blendname) + '_' + str(highest+1).zfill(3) + extension
60
61     image = bpy.data.images['Render Result']
62     if not image:
63         print('Auto Save: Render Result not found. Image not saved')
64         return
65     
66     print('Auto_Save:', save_name)
67     image.save_render(save_name, scene=None)
68
69     rndr.image_settings.file_format = original_format
70
71 ###########################################################################
72 def auto_save_UI(self, context):
73     layout = self.layout
74     
75     split=layout.split(percentage=0.66, align=True)
76     row = split.row()
77     row.prop(context.scene, 'save_after_render', text='Auto Save', toggle=False)
78     row.prop(context.scene, 'auto_save_subfolders', toggle=False)
79     #split=layout.split()
80     row=split.row()
81     row.prop(context.scene, 'auto_save_format', text='as', expand=False)
82     
83 def register():
84     bpy.types.Scene.save_after_render = BoolProperty(
85                     name='Save after render',
86                     default=True,
87                     description='Automatically save rendered images into: //auto_save/')
88     bpy.types.Scene.auto_save_format = EnumProperty(
89                     name='Auto Save File Format',
90                     description='File Format for the auto saves.',
91                     items={
92                     ('PNG', 'png', 'Save as png'),
93                     ('JPEG', 'jpg', 'Save as jpg'),
94                     ('OPEN_EXR_MULTILAYER', 'exr', 'Save as multilayer exr')},
95                     default='PNG')
96     bpy.types.Scene.auto_save_subfolders = BoolProperty(
97                     name='subfolder',
98                     default=False,
99                     description='Save into individual subfolders per blend name')
100     bpy.app.handlers.render_post.append(auto_save_render)
101     bpy.types.RENDER_PT_render.append(auto_save_UI)
102     
103 def unregister():
104     del(bpy.types.Scene.save_after_render)
105     del(bpy.types.Scene.auto_save_format)
106     del(bpy.types.Scene.auto_save_subfolders)
107     bpy.app.handlers.render_post.remove(auto_save_render)
108     bpy.types.RENDER_PT_render.remove(auto_save_UI)
109
110 if __name__ == "__main__":
111     register()