Merging with trunk up to r38631.
[blender.git] / release / scripts / startup / bl_ui / properties_scene.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 import bpy
21 from rna_prop_ui import PropertyPanel
22
23
24 class SceneButtonsPanel():
25     bl_space_type = 'PROPERTIES'
26     bl_region_type = 'WINDOW'
27     bl_context = "scene"
28
29     @classmethod
30     def poll(cls, context):
31         return context.scene
32
33
34 class SCENE_PT_scene(SceneButtonsPanel, bpy.types.Panel):
35     bl_label = "Scene"
36     COMPAT_ENGINES = {'BLENDER_RENDER'}
37
38     def draw(self, context):
39         layout = self.layout
40         scene = context.scene
41
42         layout.prop(scene, "camera")
43         layout.prop(scene, "background_set", text="Background")
44
45
46 class SCENE_PT_unit(SceneButtonsPanel, bpy.types.Panel):
47     bl_label = "Units"
48     COMPAT_ENGINES = {'BLENDER_RENDER'}
49
50     def draw(self, context):
51         layout = self.layout
52         unit = context.scene.unit_settings
53
54         col = layout.column()
55         col.row().prop(unit, "system", expand=True)
56         col.row().prop(unit, "system_rotation", expand=True)
57
58         row = layout.row()
59         row.active = (unit.system != 'NONE')
60         row.prop(unit, "scale_length", text="Scale")
61         row.prop(unit, "use_separate")
62
63
64 class SCENE_PT_keying_sets(SceneButtonsPanel, bpy.types.Panel):
65     bl_label = "Keying Sets"
66
67     def draw(self, context):
68         layout = self.layout
69
70         scene = context.scene
71         row = layout.row()
72
73         col = row.column()
74         col.template_list(scene, "keying_sets", scene.keying_sets, "active_index", rows=2)
75
76         col = row.column(align=True)
77         col.operator("anim.keying_set_add", icon='ZOOMIN', text="")
78         col.operator("anim.keying_set_remove", icon='ZOOMOUT', text="")
79         col.menu("SCENE_MT_keying_set_specials", icon='DOWNARROW_HLT', text="")
80
81         ks = scene.keying_sets.active
82         if ks and ks.is_path_absolute:
83             row = layout.row()
84
85             col = row.column()
86             col.prop(ks, "name")
87
88             subcol = col.column()
89             subcol.operator_context = 'INVOKE_DEFAULT'
90             op = subcol.operator("anim.keying_set_export", text="Export to File")
91             op.filepath = "keyingset.py"
92
93             col = row.column()
94             col.label(text="Keyframing Settings:")
95             col.prop(ks, "bl_options")
96
97
98 class SCENE_MT_keying_set_specials(bpy.types.Menu):
99     bl_label = "Keying Set Specials"
100
101     def draw(self, context):
102         layout = self.layout
103         
104         layout.operator("anim.keying_set_import", text="Import From File")
105
106 class SCENE_PT_keying_set_paths(SceneButtonsPanel, bpy.types.Panel):
107     bl_label = "Active Keying Set"
108
109     @classmethod
110     def poll(cls, context):
111         ks = context.scene.keying_sets.active
112         return (ks and ks.is_path_absolute)
113
114     def draw(self, context):
115         layout = self.layout
116
117         scene = context.scene
118         ks = scene.keying_sets.active
119
120         row = layout.row()
121         row.label(text="Paths:")
122
123         row = layout.row()
124
125         col = row.column()
126         col.template_list(ks, "paths", ks.paths, "active_index", rows=2)
127
128         col = row.column(align=True)
129         col.operator("anim.keying_set_path_add", icon='ZOOMIN', text="")
130         col.operator("anim.keying_set_path_remove", icon='ZOOMOUT', text="")
131
132         ksp = ks.paths.active
133         if ksp:
134             col = layout.column()
135             col.label(text="Target:")
136             col.template_any_ID(ksp, "id", "id_type")
137             col.template_path_builder(ksp, "data_path", ksp.id)
138
139             row = layout.row()
140
141             col = row.column()
142             col.label(text="Array Target:")
143             col.prop(ksp, "use_entire_array")
144             if ksp.use_entire_array is False:
145                 col.prop(ksp, "array_index")
146
147             col = row.column()
148             col.label(text="F-Curve Grouping:")
149             col.prop(ksp, "group_method")
150             if ksp.group_method == 'NAMED':
151                 col.prop(ksp, "group")
152
153             col.prop(ksp, "bl_options")
154
155
156 class SCENE_PT_physics(SceneButtonsPanel, bpy.types.Panel):
157     bl_label = "Gravity"
158     COMPAT_ENGINES = {'BLENDER_RENDER'}
159
160     def draw_header(self, context):
161         self.layout.prop(context.scene, "use_gravity", text="")
162
163     def draw(self, context):
164         layout = self.layout
165
166         scene = context.scene
167
168         layout.active = scene.use_gravity
169
170         layout.prop(scene, "gravity", text="")
171
172
173 class SCENE_PT_simplify(SceneButtonsPanel, bpy.types.Panel):
174     bl_label = "Simplify"
175     COMPAT_ENGINES = {'BLENDER_RENDER'}
176
177     def draw_header(self, context):
178         scene = context.scene
179         rd = scene.render
180         self.layout.prop(rd, "use_simplify", text="")
181
182     def draw(self, context):
183         layout = self.layout
184         scene = context.scene
185         rd = scene.render
186
187         layout.active = rd.use_simplify
188
189         split = layout.split()
190
191         col = split.column()
192         col.prop(rd, "simplify_subdivision", text="Subdivision")
193         col.prop(rd, "simplify_child_particles", text="Child Particles")
194
195         col.prop(rd, "use_simplify_triangulate")
196
197         col = split.column()
198         col.prop(rd, "simplify_shadow_samples", text="Shadow Samples")
199         col.prop(rd, "simplify_ao_sss", text="AO and SSS")
200
201
202 class SCENE_PT_custom_props(SceneButtonsPanel, PropertyPanel, bpy.types.Panel):
203     COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME'}
204     _context_path = "scene"
205     _property_type = bpy.types.Scene
206
207 #  XXX, move operator to op/ dir
208
209
210 class ANIM_OT_keying_set_import(bpy.types.Operator):
211     "Import Keying Set from a python script."
212     bl_idname = "anim.keying_set_import"
213     bl_label = "Import Keying Set from File"
214
215     filepath = bpy.props.StringProperty(name="File Path", description="Filepath to read file from.")
216     filter_folder = bpy.props.BoolProperty(name="Filter folders", description="", default=True, options={'HIDDEN'})
217     filter_text = bpy.props.BoolProperty(name="Filter text", description="", default=True, options={'HIDDEN'})
218     filter_python = bpy.props.BoolProperty(name="Filter python", description="", default=True, options={'HIDDEN'})
219
220     def execute(self, context):
221         if not self.filepath:
222             raise Exception("Filepath not set.")
223         
224         f = open(self.filepath, "r")
225         if not f:
226             raise Exception("Could not open file.")
227         
228         # lazy way of loading and running this file...
229         exec(compile(f.read(), self.filepath, 'exec'))
230         
231         f.close()
232
233         return {'FINISHED'}
234
235     def invoke(self, context, event):
236         wm = context.window_manager
237         wm.fileselect_add(self)
238         return {'RUNNING_MODAL'}
239
240 class ANIM_OT_keying_set_export(bpy.types.Operator):
241     "Export Keying Set to a python script."
242     bl_idname = "anim.keying_set_export"
243     bl_label = "Export Keying Set..."
244
245     filepath = bpy.props.StringProperty(name="File Path", description="Filepath to write file to.")
246     filter_folder = bpy.props.BoolProperty(name="Filter folders", description="", default=True, options={'HIDDEN'})
247     filter_text = bpy.props.BoolProperty(name="Filter text", description="", default=True, options={'HIDDEN'})
248     filter_python = bpy.props.BoolProperty(name="Filter python", description="", default=True, options={'HIDDEN'})
249
250     def execute(self, context):
251         if not self.filepath:
252             raise Exception("Filepath not set.")
253
254         f = open(self.filepath, "w")
255         if not f:
256             raise Exception("Could not open file.")
257
258         scene = context.scene
259         ks = scene.keying_sets.active
260
261         f.write("# Keying Set: %s\n" % ks.name)
262
263         f.write("import bpy\n\n")
264         f.write("scene= bpy.data.scenes[0]\n\n")  # XXX, why not use the current scene?
265
266         # Add KeyingSet and set general settings
267         f.write("# Keying Set Level declarations\n")
268         f.write("ks= scene.keying_sets.new(name=\"%s\")\n" % ks.name)
269
270         if not ks.is_path_absolute:
271             f.write("ks.is_path_absolute = False\n")
272         f.write("\n")
273
274         f.write("ks.bl_options = %r\n" % ks.bl_options)
275         f.write("\n")
276
277         # generate and write set of lookups for id's used in paths
278         id_to_paths_cache = {}  # cache for syncing ID-blocks to bpy paths + shorthands
279
280         for ksp in ks.paths:
281             if ksp.id is None:
282                 continue
283             if ksp.id in id_to_paths_cache:
284                 continue
285
286             # - idtype_list is used to get the list of id-datablocks from bpy.data.*
287             #   since this info isn't available elsewhere
288             # - id.bl_rna.name gives a name suitable for UI,
289             #   with a capitalised first letter, but we need
290             #   the plural form that's all lower case
291             idtype_list = ksp.id.bl_rna.name.lower() + "s"
292             id_bpy_path = "bpy.data.%s[\"%s\"]" % (idtype_list, ksp.id.name)
293
294             # shorthand ID for the ID-block (as used in the script)
295             short_id = "id_%d" % len(id_to_paths_cache)
296
297             # store this in the cache now
298             id_to_paths_cache[ksp.id] = [short_id, id_bpy_path]
299
300         f.write("# ID's that are commonly used\n")
301         for id_pair in id_to_paths_cache.values():
302             f.write("%s = %s\n" % (id_pair[0], id_pair[1]))
303         f.write("\n")
304
305         # write paths
306         f.write("# Path Definitions\n")
307         for ksp in ks.paths:
308             f.write("ksp = ks.paths.add(")
309
310             # id-block + data_path
311             if ksp.id:
312                 # find the relevant shorthand from the cache
313                 id_bpy_path = id_to_paths_cache[ksp.id][0]
314             else:
315                 id_bpy_path = "None"  # XXX...
316             f.write("%s, '%s'" % (id_bpy_path, ksp.data_path))
317
318             # array index settings (if applicable)
319             if ksp.use_entire_array:
320                 f.write(", index=-1")
321             else:
322                 f.write(", index=%d" % ksp.array_index)
323
324             # grouping settings (if applicable)
325             # NOTE: the current default is KEYINGSET, but if this changes, change this code too
326             if ksp.group_method == 'NAMED':
327                 f.write(", group_method='%s', group_name=\"%s\"" % (ksp.group_method, ksp.group))
328             elif ksp.group_method != 'KEYINGSET':
329                 f.write(", group_method='%s'" % ksp.group_method)
330
331             # finish off
332             f.write(")\n")
333
334         f.write("\n")
335         f.close()
336
337         return {'FINISHED'}
338
339     def invoke(self, context, event):
340         wm = context.window_manager
341         wm.fileselect_add(self)
342         return {'RUNNING_MODAL'}
343
344 if __name__ == "__main__":  # only for live edit.
345     bpy.utils.register_module(__name__)