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