Source code for ddg.visualization.blender.animation

import bpy
from typing import Sequence, Dict, Any


[docs]def set_keyframe( bpy_struct: Any, frame: int, property_name: str, value: Any, action_group: str = "" ) -> bool: """Add a keyframe value a frame to a property of a bpy_struct. Parameters ---------- bpy_struct : A Blender bpy_struct object which properties to animate. frame : An Integer. The frame at which the value will get inserted. property_name : A String representing the name of property to change. value : Any value the property should get at this frame. action_group : A String. Name of the ActionGroup the F-Curve of the animation should be added to. Returns ------- True if the keyframe was set successfully, else False. """ setattr(bpy_struct, property_name, value) return bpy_struct.keyframe_insert(property_name, frame=frame, group=action_group)
[docs]def animate_property( bpy_struct: Any, property_name: str, frames: Sequence[int], values: Sequence[Any], action_group: str = "", ) -> bool: """Add keyframes to a property of a bpy_struct. Parameters ---------- bpy_struct : A Blender bpy_struct object which properties to animate. frames : A Sequence of integers. Correspond to the frames at which the values will get inserted. values : A Sequence. The values the property should take for each corresponding frame. action_group : A String. Name of the ActionGroup the F-Curve of the animation should be added to. Returns ------- True if all keyframes were set successfully, else False. """ # Check if the sequences of values have the right length. if len(values) != len(frames): raise ValueError( "Values sequence must have the same length as the frames " "sequence. Found values sequence of length " f'{len(values)} for property "{property_name}", ' f"but frames sequence has length {len(frames)}.", ) return_values = ( set_keyframe(bpy_struct, frame, property_name, value, action_group=action_group) for value, frame in zip(values, frames) ) return all(return_values)
[docs]def animate_properties( bpy_struct: Any, frames: Sequence[int], animation: Dict[str, Sequence[Any]], action_group: str = "", ) -> bool: """Add keyframes to multiple properties of a bpy_struct. For each frame in frames, each property from bpy_struct listed in the keys of animation gets assigned a value corresponding to that frame. Parameters ---------- bpy_struct : A Blender bpy_struct object which properties to animate. frames : A Sequence of integers. Correspond to the frames at which the values will get inserted. animation : Dict[str, Sequence[Any]]. Associates a property_name to a sequence of values. action_group : A String. Name of the ActionGroup the F-Curve of the animation should be added to. Returns ------- True if all keyframes were set successfully, else False. """ return_values = ( animate_property( bpy_struct, property_name, frames, values_sequence, action_group=action_group, ) for property_name, values_sequence in animation.items() ) return all(return_values)
[docs]def clear_animation_data(bpy_struct: Any) -> None: bpy_struct.animation_data_clear()
[docs]def set_end_frame(frame: int) -> None: bpy.context.scene.frame_end = frame