These scripts enable you to play smooth 2D/3D Unity animations in Python.
It is particularly useful when you need to animate window elements, create GUI effects, or preview Unity animations outside the Unity Editor.
- Parse and play Unity
.animfiles in Python - Support for Position, Rotation, Scale, and Euler curves
- Full Hermite spline interpolation with weight support
- Animation event system with callback registration
- JIT acceleration with numba for high performance
- PySide6 integration for real-time GUI animation
- Interactive debugging panel with real-time parameter adjustment
- Multiple output formats (Pygame, PySide6, QML)
Run the example selector:
python example.pyAvailable examples:
interactive_panel- Interactive animation debugging panelpygame_viewer- Pygame-based animation viewerpyside_popup_window- PySide6 popup window animationqml_window- QML animation window (ball animation, button scaling)
from unity_animation_player import AnimationPlayer
# Load animation
player = AnimationPlayer("examples/AnimationClip/T.anim")
# Get animation state at 0.5 seconds
result, valid = player.play_frame(0.5, path="general")
if valid:
print(f"Position: {result.get('position')}")
print(f"Scale: {result.get('scale')}")
print(f"Rotation: {result.get('rotation')}")Control animation output with flexible parameters:
# Map Unity coordinates to screen coordinates
result, valid = player.play_frame(0.5,
position_unit=('x', 'y'),
position_ratio=(19.2, 10.8), # Scale X and Y independently
position_reverse=(False, True) # Reverse Y axis
)from PySide6.QtCore import Signal
from unity_animation_player import PysideAnimationPlayer
class MyWindow(QWidget):
anim_signal = Signal(dict)
def __init__(self):
super().__init__()
self.player = PysideAnimationPlayer(
self.anim_signal,
"animation.anim"
)
self.anim_signal.connect(self.on_frame)
self.player.play()
def on_frame(self, data):
if data.get('playable'):
pos = data.get('position')
self.move(*pos)Register callbacks for Unity animation events:
def on_event(data, float_param):
print(f"Event: {data}, {float_param}")
player.register_event('eventTriggered', on_event, ('data', 'floatParameter'))- SHA256-based JSON caching for fast reloading
- Numba JIT compilation for core interpolation
- PyYAML CLoader for fast YAML parsing
- Python >= 3.8
- numpy
- PyYAML
- PySide6 (for GUI features)
- numba (optional, for JIT acceleration)
pip install numpy pyyaml PySide6For optimal performance:
pip install numba- Rational Bezier interpolation - Full Unity curve matching
- Weight mode support - Now fully implemented
- Independent axis control - Per-axis unit, reverse, and ratio
- Event time reversal - Events trigger correctly when playing in reverse
- Dynamic event addition - Add custom events at runtime
The initial version had limitations with weight mode support. The current version fully implements Unity's interpolation system, including weighted Bezier curves and proper handling of infinite slopes.
MIT License
Issues and pull requests are welcome.
Detailed Documentation in https://ljcyounger.github.io/UnityAnimationPlayer_python/