Fix some dummy formating issue (breacks i18n message processing).
[blender.git] / release / scripts / startup / bl_operators / anim.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-80 compliant>
20
21 if "bpy" in locals():
22     import imp
23     if "anim_utils" in locals():
24         imp.reload(anim_utils)
25
26
27 import bpy
28 from bpy.types import Operator
29 from bpy.props import (IntProperty,
30                        BoolProperty,
31                        EnumProperty,
32                        StringProperty,
33                        )
34
35
36 class ANIM_OT_keying_set_export(Operator):
37     "Export Keying Set to a python script"
38     bl_idname = "anim.keying_set_export"
39     bl_label = "Export Keying Set..."
40
41     filepath = StringProperty(
42             subtype='FILE_PATH',
43             )
44     filter_folder = BoolProperty(
45             name="Filter folders",
46             default=True,
47             options={'HIDDEN'},
48             )
49     filter_text = BoolProperty(
50             name="Filter text",
51             default=True,
52             options={'HIDDEN'},
53             )
54     filter_python = BoolProperty(
55             name="Filter python",
56             default=True,
57             options={'HIDDEN'},
58             )
59
60     def execute(self, context):
61         if not self.filepath:
62             raise Exception("Filepath not set")
63
64         f = open(self.filepath, "w")
65         if not f:
66             raise Exception("Could not open file")
67
68         scene = context.scene
69         ks = scene.keying_sets.active
70
71         f.write("# Keying Set: %s\n" % ks.bl_idname)
72
73         f.write("import bpy\n\n")
74         f.write("scene = bpy.context.scene\n\n")
75
76         # Add KeyingSet and set general settings
77         f.write("# Keying Set Level declarations\n")
78         f.write("ks = scene.keying_sets.new(idname=\"%s\", name=\"%s\")\n"
79                 "" % (ks.bl_idname, ks.bl_label))
80         f.write("ks.bl_description = \"%s\"\n" % ks.bl_description)
81
82         if not ks.is_path_absolute:
83             f.write("ks.is_path_absolute = False\n")
84         f.write("\n")
85
86         f.write("ks.bl_options = %r\n" % ks.bl_options)
87         f.write("\n")
88
89         # --------------------------------------------------------
90         # generate and write set of lookups for id's used in paths
91
92         # cache for syncing ID-blocks to bpy paths + shorthand's
93         id_to_paths_cache = {}
94
95         for ksp in ks.paths:
96             if ksp.id is None:
97                 continue
98             if ksp.id in id_to_paths_cache:
99                 continue
100
101             """
102             - idtype_list is used to get the list of id-datablocks from
103               bpy.data.* since this info isn't available elsewhere
104             - id.bl_rna.name gives a name suitable for UI,
105               with a capitalised first letter, but we need
106               the plural form that's all lower case
107             """
108
109             idtype_list = ksp.id.bl_rna.name.lower() + "s"
110             id_bpy_path = "bpy.data.%s[\"%s\"]" % (idtype_list, ksp.id.name)
111
112             # shorthand ID for the ID-block (as used in the script)
113             short_id = "id_%d" % len(id_to_paths_cache)
114
115             # store this in the cache now
116             id_to_paths_cache[ksp.id] = [short_id, id_bpy_path]
117
118         f.write("# ID's that are commonly used\n")
119         for id_pair in id_to_paths_cache.values():
120             f.write("%s = %s\n" % (id_pair[0], id_pair[1]))
121         f.write("\n")
122
123         # write paths
124         f.write("# Path Definitions\n")
125         for ksp in ks.paths:
126             f.write("ksp = ks.paths.add(")
127
128             # id-block + data_path
129             if ksp.id:
130                 # find the relevant shorthand from the cache
131                 id_bpy_path = id_to_paths_cache[ksp.id][0]
132             else:
133                 id_bpy_path = "None"  # XXX...
134             f.write("%s, '%s'" % (id_bpy_path, ksp.data_path))
135
136             # array index settings (if applicable)
137             if ksp.use_entire_array:
138                 f.write(", index=-1")
139             else:
140                 f.write(", index=%d" % ksp.array_index)
141
142             # grouping settings (if applicable)
143             # NOTE: the current default is KEYINGSET, but if this changes,
144             # change this code too
145             if ksp.group_method == 'NAMED':
146                 f.write(", group_method='%s', group_name=\"%s\"" %
147                         (ksp.group_method, ksp.group))
148             elif ksp.group_method != 'KEYINGSET':
149                 f.write(", group_method='%s'" % ksp.group_method)
150
151             # finish off
152             f.write(")\n")
153
154         f.write("\n")
155         f.close()
156
157         return {'FINISHED'}
158
159     def invoke(self, context, event):
160         wm = context.window_manager
161         wm.fileselect_add(self)
162         return {'RUNNING_MODAL'}
163
164
165 class BakeAction(Operator):
166     """Bake object/pose loc/scale/rotation animation to a new action"""
167     bl_idname = "nla.bake"
168     bl_label = "Bake Action"
169     bl_options = {'REGISTER', 'UNDO'}
170
171     frame_start = IntProperty(
172             name="Start Frame",
173             description="Start frame for baking",
174             min=0, max=300000,
175             default=1,
176             )
177     frame_end = IntProperty(
178             name="End Frame",
179             description="End frame for baking",
180             min=1, max=300000,
181             default=250,
182             )
183     step = IntProperty(
184             name="Frame Step",
185             description="Frame Step",
186             min=1, max=120,
187             default=1,
188             )
189     only_selected = BoolProperty(
190             name="Only Selected",
191             description="Only key selected object/bones",
192             default=True,
193             )
194     visual_keying = BoolProperty(
195             name="Visual Keying",
196             description="Keyframe from the final transformations (with constraints applied)",
197             default=False,
198             )
199     clear_constraints = BoolProperty(
200             name="Clear Constraints",
201             description="Remove all constraints from keyed object/bones, and do 'visual' keying",
202             default=False,
203             )
204     clear_parents = BoolProperty(
205             name="Clear Parents",
206             description="Bake animation onto the object then clear parents (objects only)",
207             default=False,
208             )
209     bake_types = EnumProperty(
210             name="Bake Data",
211             description="Which data's transformations to bake",
212             options={'ENUM_FLAG'},
213             items=(('POSE', "Pose", "Bake bones transformations"),
214                    ('OBJECT', "Object", "Bake object transformations"),
215                    ),
216             default={'POSE'},
217             )
218
219     def execute(self, context):
220
221         from bpy_extras import anim_utils
222
223         action = anim_utils.bake_action(self.frame_start,
224                                         self.frame_end,
225                                         frame_step=self.step,
226                                         only_selected=self.only_selected,
227                                         do_pose='POSE' in self.bake_types,
228                                         do_object='OBJECT' in self.bake_types,
229                                         do_visual_keying=self.visual_keying,
230                                         do_constraint_clear=self.clear_constraints,
231                                         do_parents_clear=self.clear_parents,
232                                         do_clean=True,
233                                         )
234
235         if action is None:
236             self.report({'INFO'}, "Nothing to bake")
237             return {'CANCELLED'}
238
239         return {'FINISHED'}
240
241     def invoke(self, context, event):
242         scene = context.scene
243         self.frame_start = scene.frame_start
244         self.frame_end = scene.frame_end
245         self.bake_types = {'POSE'} if context.mode == 'POSE' else {'OBJECT'}
246
247         wm = context.window_manager
248         return wm.invoke_props_dialog(self)
249
250
251 class ClearUselessActions(Operator):
252     """Mark actions with no F-Curves for deletion after save & reload of """ \
253     """file preserving \"action libraries\""""
254     bl_idname = "anim.clear_useless_actions"
255     bl_label = "Clear Useless Actions"
256     bl_options = {'REGISTER', 'UNDO'}
257
258     only_unused = BoolProperty(name="Only Unused",
259             description="Only unused (Fake User only) actions get considered",
260             default=True)
261
262     @classmethod
263     def poll(cls, context):
264         return bool(bpy.data.actions)
265
266     def execute(self, context):
267         removed = 0
268
269         for action in bpy.data.actions:
270             # if only user is "fake" user...
271             if ((self.only_unused is False) or
272                 (action.use_fake_user and action.users == 1)):
273
274                 # if it has F-Curves, then it's a "action library"
275                 # (i.e. walk, wave, jump, etc.)
276                 # and should be left alone as that's what fake users are for!
277                 if not action.fcurves:
278                     # mark action for deletion
279                     action.user_clear()
280                     removed += 1
281
282         self.report({'INFO'}, "Removed %d empty and/or fake-user only Actions"
283                               % removed)
284         return {'FINISHED'}
285
286
287 class UpdateAnimatedTransformConstraint(Operator):
288     """Update fcurves/drivers affecting Transform constraints (use it with files from 2.70 and earlier)"""
289     bl_idname = "anim.update_animated_transform_constraints"
290     bl_label = "Update Animated Transform Constraints"
291     bl_options = {'REGISTER', 'UNDO'}
292
293     use_convert_to_radians = BoolProperty(
294             name="Convert To Radians",
295             description="Convert fcurves/drivers affecting rotations to radians (Warning: use this only once!)",
296             default=True,
297             )
298
299     def execute(self, context):
300         import animsys_refactor
301         from math import radians
302         import io
303
304         from_paths = {"from_max_x", "from_max_y", "from_max_z", "from_min_x", "from_min_y", "from_min_z"}
305         to_paths = {"to_max_x", "to_max_y", "to_max_z", "to_min_x", "to_min_y", "to_min_z"}
306         paths = from_paths | to_paths
307
308         def update_cb(base, class_name, old_path, fcurve, options):
309             print(options)
310             def handle_deg2rad(fcurve):
311                 if fcurve is not None:
312                     if hasattr(fcurve, "keyframes"):
313                         for k in fcurve.keyframes:
314                             k.co.y = radians(k.co.y)
315                     for mod in fcurve.modifiers:
316                         if mod.type == 'GENERATOR':
317                             if mod.mode == 'POLYNOMIAL':
318                                 mod.coefficients[:] = [radians(c) for c in mod.coefficients]
319                             else:  # if mod.type == 'POLYNOMIAL_FACTORISED':
320                                 mod.coefficients[:2] = [radians(c) for c in mod.coefficients[:2]]
321                         elif mod.type == 'FNGENERATOR':
322                             mod.amplitude = radians(mod.amplitude)
323                     fcurve.update()
324
325             data = ...
326             try:
327                 data = eval("base." + old_path)
328             except:
329                 pass
330             ret = (data, old_path)
331             if isinstance(base, bpy.types.TransformConstraint) and data is not ...:
332                 new_path = None
333                 map_info = base.map_from if old_path in from_paths else base.map_to
334                 if map_info == 'ROTATION':
335                     new_path = old_path + "_rot"
336                     if options is not None and options["use_convert_to_radians"]:
337                         handle_deg2rad(fcurve)
338                 elif map_info == 'SCALE':
339                     new_path = old_path + "_scale"
340
341                 if new_path is not None:
342                     data = ...
343                     try:
344                         data = eval("base." + new_path)
345                     except:
346                         pass
347                     ret = (data, new_path)
348                     #print(ret)
349
350             return ret
351
352         options = {"use_convert_to_radians": self.use_convert_to_radians}
353         replace_ls = [("TransformConstraint", p, update_cb, options) for p in paths]
354         log = io.StringIO()
355
356         animsys_refactor.update_data_paths(replace_ls, log)
357
358         context.scene.frame_set(context.scene.frame_current)
359
360         log = log.getvalue()
361         if log:
362             print(log)
363             text = bpy.data.texts.new("UpdateAnimatedTransformConstraint Report")
364             text.from_string(log)
365             self.report({'INFO'}, "Complete report available on '{}' text datablock".format(text.name))
366         return {'FINISHED'}