anim.py 15 KB

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