minor code cleanup
[blender-addons-contrib.git] / render_to_print.py
1 # ##### BEGIN GPL LICENSE BLOCK #####
2 #
3 #  This program is free software; you can redistribute it and/or
4 #  modify it under the terms of the GNU General Public License
5 #  as published by the Free Software Foundation; either version 2
6 #  of the License, or (at your option) any later version.
7 #
8 #  This program is distributed in the hope that it will be useful,
9 #  but WITHOUT ANY WARRANTY; without even the implied warranty of
10 #  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11 #  GNU General Public License for more details.
12 #
13 #  You should have received a copy of the GNU General Public License
14 #  along with this program; if not, write to the Free Software Foundation,
15 #  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 #
17 # ##### END GPL LICENSE BLOCK #####
18
19 # <pep8 compliant>
20
21 bl_info = {
22     'name': 'Render to Print',
23     'author': 'Marco Crippa <thekrypt77@tiscali.it>, Dealga McArdle',
24     'version': (0, 2),
25     'blender': (2, 5, 8),
26     'location': 'Render > Render to Print',
27     'description': 'Set the size of the render for a print',
28     'wiki_url': 'http://wiki.blender.org/index.php/Extensions:2.6/Py/'\
29         'Scripts/Render/Render to Print',
30     'tracker_url': 'https://projects.blender.org/tracker/index.php?'\
31         'func=detail&aid=24219',
32     'category': 'Render'}
33
34
35 import math
36 import bpy
37 from bpy.types import Panel, Operator, Scene, PropertyGroup
38 from bpy.props import (IntProperty,
39                        FloatProperty,
40                        StringProperty,
41                        EnumProperty,
42                        PointerProperty,
43                        )
44
45
46 paper_presets = (
47     ("custom_1_1", "custom", ""),
48     ("A0_84.1_118.9", "A0 (84.1x118.9 cm)", ""),
49     ("A1_59.4_84.1", "A1 (59.4x84.1 cm)", ""),
50     ("A2_42.0_59.4", "A2 (42.0x59.4 cm)", ""),
51     ("A3_29.7_42.0", "A3 (29.7 42.0 cm)", ""),
52     ("A4_21.0_29.7", "A4 (21.0x29.7 cm)", ""),
53     ("A5_14.8_21.0", "A5 (14.8x21.0 cm)", ""),
54     ("A6_10.5_14.8", "A6 (10.5x14.8 cm)", ""),
55     ("A7_7.4_10.5", "A7 (7.4x10.5 cm)", ""),
56     ("A8_5.2_7.4", "A8 (5.2x7.4 cm)", ""),
57     ("A9_3.7_5.2", "A9 (3.7x5.2 cm)", ""),
58     ("A10_2.6_3.7", "A10 (2.6x3.7 cm)", ""),
59
60     ("B0_100.0_141.4", "B0 (100.0x141.4 cm)", ""),
61     ("B1_70.7_100.0", "B1 (70.7x100.0 cm)", ""),
62     ("B2_50.0_70.7", "B2 (50.0x70.7 cm)", ""),
63     ("B3_35.3_50.0", "B3 (35.3x50.0 cm)", ""),
64     ("B4_25.0_35.3", "B4 (25.0x35.3 cm)", ""),
65     ("B5_17.6_25.0", "B5 (17.6x25.0 cm)", ""),
66     ("B6_12.5_17.6", "B6 (12.5x17.6 cm)", ""),
67     ("B7_8.8_12.5", "B7 (8.8x12.5 cm)", ""),
68     ("B8_6.2_8.8", "B8 (6.2x8.8 cm)", ""),
69     ("B9_4.4_6.2", "B9 (4.4x6.2 cm)", ""),
70     ("B10_3.1_4.4", "B10 (3.1x4.4 cm)", ""),
71
72     ("C0_91.7_129.7", "C0 (91.7x129.7 cm)", ""),
73     ("C1_64.8_91.7", "C1 (64.8x91.7 cm)", ""),
74     ("C2_45.8_64.8", "C2 (45.8x64.8 cm)", ""),
75     ("C3_32.4_45.8", "C3 (32.4x45.8 cm)", ""),
76     ("C4_22.9_32.4", "C4 (22.9x32.4 cm)", ""),
77     ("C5_16.2_22.9", "C5 (16.2x22.9 cm)", ""),
78     ("C6_11.4_16.2", "C6 (11.4x16.2 cm)", ""),
79     ("C7_8.1_11.4", "C7 (8.1x11.4 cm)", ""),
80     ("C8_5.7_8.1", "C8 (5.7x8.1 cm)", ""),
81     ("C9_4.0_5.7", "C9 (4.0x5.7 cm)", ""),
82     ("C10_2.8_4.0", "C10 (2.8x4.0 cm)", ""),
83
84     ("Letter_21.6_27.9", "Letter (21.6x27.9 cm)", ""),
85     ("Legal_21.6_35.6", "Legal (21.6x35.6 cm)", ""),
86     ("Legal junior_20.3_12.7", "Legal junior (20.3x12.7 cm)", ""),
87     ("Ledger_43.2_27.9", "Ledger (43.2x27.9 cm)", ""),
88     ("Tabloid_27.9_43.2", "Tabloid (27.9x43.2 cm)", ""),
89
90     ("ANSI C_43.2_55.9", "ANSI C (43.2x55.9 cm)", ""),
91     ("ANSI D_55.9_86.4", "ANSI D (55.9x86.4 cm)", ""),
92     ("ANSI E_86.4_111.8", "ANSI E (86.4x111.8 cm)", ""),
93
94     ("Arch A_22.9_30.5", "Arch A (22.9x30.5 cm)", ""),
95     ("Arch B_30.5_45.7", "Arch B (30.5x45.7 cm)", ""),
96     ("Arch C_45.7_61.0", "Arch C (45.7x61.0 cm)", ""),
97     ("Arch D_61.0_91.4", "Arch D (61.0x91.4 cm)", ""),
98     ("Arch E_91.4_121.9", "Arch E (91.4x121.9 cm)", ""),
99     ("Arch E1_76.2_106.7", "Arch E1 (76.2x106.7 cm)", ""),
100     ("Arch E2_66.0_96.5", "Arch E2 (66.0x96.5 cm)", ""),
101     ("Arch E3_68.6_99.1", "Arch E3 (68.6x99.1 cm)", ""),
102     )
103
104
105 def paper_enum_parse(idname):
106     tipo, dim_w, dim_h = idname.split("_")
107     return tipo, float(dim_w), float(dim_h)
108
109
110 paper_presets_data = {idname: paper_enum_parse(idname)
111                       for idname, name, descr in paper_presets}
112
113
114 def update_settings_cb(self, context):
115     # annoying workaround for recursive call
116     if update_settings_cb.level == False:
117         update_settings_cb.level = True
118         pixels_from_print(self)
119         update_settings_cb.level = False
120
121 update_settings_cb.level = False
122
123
124 class RenderPrintSertings(PropertyGroup):
125     unit_from = EnumProperty(
126             name="Set from",
127             description="Set from",
128             items=(
129                 ("CM_TO_PIXELS", "CM -> Pixel", "Centermeters to Pixels"),
130                 ("PIXELS_TO_CM", "Pixel -> CM", "Pixels to Centermeters")
131                 ),
132             default="CM_TO_PIXELS",
133             )
134     orientation = EnumProperty(
135             name="Page Orientation",
136             description="Set orientation",
137             items=(
138                 ("Portrait", "Portrait", "Portrait"),
139                 ("Landscape", "Landscape", "Landscape")
140             ),
141             default="Portrait",
142             update=update_settings_cb,
143             )
144     preset = EnumProperty(
145             name="Select Preset",
146             description="Select from preset",
147             items=paper_presets,
148             default="custom_1_1",
149             update=update_settings_cb,
150             )
151     dpi = IntProperty(
152             name="DPI",
153             description="Dots per Inch",
154             default=300,
155             min=72, max=1800,
156             update=update_settings_cb,
157             )
158     width_cm = FloatProperty(
159             name="Width",
160             description="Width in CM",
161             default=5.0,
162             min=1.0, max=100000.0,
163             update=update_settings_cb,
164             )
165     height_cm = FloatProperty(
166             name="Height",
167             description="Height in CM",
168             default=3.0,
169             min=1.0, max=100000.0,
170             update=update_settings_cb,
171             )
172     width_px = IntProperty(
173             name="Pixel Width",
174             description="Pixel Width",
175             default=900,
176             min=4, max=10000,
177             update=update_settings_cb,
178             )
179     height_px = IntProperty(
180             name="Pixel Height",
181             description="Pixel Height",
182             default=600,
183             min=4, max=10000,
184             update=update_settings_cb,
185             )
186
187
188 def pixels_from_print(ps):
189     tipo, dim_w, dim_h = paper_presets_data[ps.preset]
190
191     if ps.unit_from == "CM_TO_PIXELS":
192         if tipo == "custom":
193             dim_w = ps.width_cm
194             dim_h = ps.height_cm
195             ps.width_cm = dim_w
196             ps.height_cm = dim_h
197         elif tipo != "custom" and ps.orientation == "Landscape":
198             ps.width_cm = dim_h
199             ps.height_cm = dim_w
200         elif tipo != "custom" and ps.orientation == "Portrait":
201             ps.width_cm = dim_w
202             ps.height_cm = dim_h
203
204         ps.width_px = math.ceil((ps.width_cm * ps.dpi) / 2.54)
205         ps.height_px = math.ceil((ps.height_cm * ps.dpi) / 2.54)
206     else:
207         if tipo != "custom" and ps.orientation == "Landscape":
208             ps.width_cm = dim_h
209             ps.height_cm = dim_w
210             ps.width_px = math.ceil((ps.width_cm * ps.dpi) / 2.54)
211             ps.height_px = math.ceil((ps.height_cm * ps.dpi) / 2.54)
212         elif tipo != "custom" and ps.orientation == "Portrait":
213             ps.width_cm = dim_w
214             ps.height_cm = dim_h
215             ps.width_px = math.ceil((ps.width_cm * ps.dpi) / 2.54)
216             ps.height_px = math.ceil((ps.height_cm * ps.dpi) / 2.54)
217
218         ps.width_cm = (ps.width_px / ps.dpi) * 2.54
219         ps.height_cm = (ps.height_px / ps.dpi) * 2.54
220
221
222 class RENDER_PT_print(Panel):
223     bl_label = "Render to Print"
224     bl_space_type = 'PROPERTIES'
225     bl_region_type = 'WINDOW'
226     bl_context = 'render'
227
228     def draw(self, context):
229
230         layout = self.layout
231         scene = context.scene
232         ps = scene.print_settings
233
234         row = layout.row(align=True)
235         row1 = layout.row(align=True)
236         row2 = layout.row(align=True)
237         row3 = layout.row(align=True)
238         row4 = layout.row(align=True)
239         row5 = layout.row(align=True)
240         row6 = layout.row(align=True)
241         row7 = layout.row(align=True)
242         col = layout.column(align=True)
243
244         row.prop(ps, "unit_from")
245         row1.prop(ps, "orientation")
246         row2.prop(ps, "preset")
247
248         col.separator()
249         row3.prop(ps, "width_cm")
250         row3.separator()
251         row3.prop(ps, "height_cm")
252         col.separator()
253         row4.prop(ps, "dpi")
254         col.separator()
255         row5.prop(ps, "width_px")
256         row5.separator()
257         row5.prop(ps, "height_px")
258
259         col.separator()
260         row6.label("Inch Width: %.2f" % (ps.width_cm / 2.54))
261         row6.label("Inch Height: %.2f" % (ps.height_cm / 2.54))
262         col.separator()
263
264         row7.operator("render.apply_size", icon="RENDER_STILL")
265
266         #  this if else deals with hiding UI elements when logic demands it.
267         tipo = paper_presets_data[ps.preset][0]
268
269         if tipo != "custom":
270             row.active = False
271             row.enabled = False
272
273         if ps.unit_from == "CM_TO_PIXELS":
274             row5.active = False
275             row5.enabled = False
276
277             if tipo == "custom":
278                 row3.active = True
279                 row3.enabled = True
280                 row1.active = False
281                 row1.enabled = False
282             elif tipo != "custom" and ps.orientation == "Landscape":
283                 row3.active = False
284                 row3.enabled = False
285                 row1.active = True
286                 row1.enabled = True
287             elif tipo != "custom" and ps.orientation == "Portrait":
288                 row3.active = False
289                 row3.enabled = False
290                 row1.active = True
291                 row1.enabled = True
292         else:
293             row3.active = False
294             row3.enabled = False
295
296             if tipo == "custom":
297                 row1.active = False
298                 row1.enabled = False
299             elif tipo != "custom" and ps.orientation == "Landscape":
300                 row1.active = True
301                 row1.enabled = True
302                 row5.active = False
303                 row5.enabled = False
304             elif tipo != "custom" and ps.orientation == "Portrait":
305                 row1.active = True
306                 row1.enabled = True
307                 row5.active = False
308                 row5.enabled = False
309
310
311 class RENDER_OT_apply_size(Operator):
312     bl_idname = "render.apply_size"
313     bl_label = "Apply Print to Render"
314     bl_description = "Set the render dimension"
315
316     def execute(self, context):
317
318         scene = context.scene
319         ps = scene.print_settings
320
321         pixels_from_print(ps)
322
323         render = scene.render
324         render.resolution_x = ps.width_px
325         render.resolution_y = ps.height_px
326
327         return {'FINISHED'}
328
329
330 def register():
331     bpy.utils.register_class(RENDER_OT_apply_size)
332     bpy.utils.register_class(RENDER_PT_print)
333     bpy.utils.register_class(RenderPrintSertings)
334
335     Scene.print_settings = PointerProperty(type=RenderPrintSertings)
336
337
338 def unregister():
339     bpy.utils.unregister_class(RENDER_OT_apply_size)
340     bpy.utils.unregister_class(RENDER_PT_print)
341     bpy.utils.unregister_class(RenderPrintSertings)
342     del Scene.print_settings
343
344
345 if __name__ == "__main__":
346     register()