This repository has been archived by the owner on Jul 1, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.py
204 lines (158 loc) · 7.67 KB
/
animation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
from __future__ import annotations # type hinting the self reference
import chess
import chess.pgn
import bpy
from mathutils import Vector
from math import radians
from typing import Tuple, Dict, List, Optional
def square_to_world_space(square: int) -> Tuple[float, float]:
return ((square % 8) + 0.5, (square // 8) + 0.5)
def keyframes(array: List[Optional[CustomPiece]]):
for piece in array:
if piece is not None:
piece.keyframe_insert(data_path="location", index=-1)
class CustomPiece():
def __init__(self, pieceType: chess.Piece, blender_obj: bpy.types.Object,\
array: List[Optional[CustomPiece]], loc: int):
self._pieceType = pieceType.piece_type # int
self._colour = pieceType.color # bool
self._blender_obj = blender_obj.copy()
self._array = array # reference to array containing self
self._inital_loc = loc
self._loc = loc # int (1d array index)
x, y = square_to_world_space(self._loc)
self._blender_obj.location = Vector((x, y, 0.3))
# set material based on colour
if self._colour:
self._mat = bpy.data.materials["White pieces"]
else:
self._mat = bpy.data.materials["Black pieces"]
self._blender_obj.active_material = self._mat
if self._colour and self._pieceType == chess.KNIGHT:
self._blender_obj.rotation_euler[2] = radians(180) #XYZ
# add object to collection so its visable
bpy.data.collections[['Black', 'White'][self._colour]].objects.link(self._blender_obj)
def move(self, new_loc: int, zTo: float = 0.3):
xTo, yTo = square_to_world_space(new_loc)
self._blender_obj.location = Vector((xTo, yTo, zTo))
print("Moved to ", self._blender_obj.location)
self._array[new_loc] = self
self._array[self._loc] = None
self._loc = new_loc
def die(self) -> CustomPiece:
self._array[self._loc] = None
self.keyframe_insert(data_path="location", frame=FRAME_COUNT-6)
xTo, yTo = square_to_world_space(self._loc)
self._blender_obj.location = Vector((xTo, yTo, 2.1))
self.keyframe_insert(data_path="location", frame=FRAME_COUNT+3)
if self._colour:
self._inital_loc += -16
else:
self._inital_loc += 16
xTo, yTo = square_to_world_space(self._inital_loc)
self._blender_obj.location = Vector((xTo, yTo, 2.1))
self.keyframe_insert(data_path="location", frame=FRAME_COUNT+21)
xTo, yTo = square_to_world_space(self._inital_loc)
self._blender_obj.location = Vector((xTo, yTo, 0.1))
self.keyframe_insert(data_path="location", frame=FRAME_COUNT+29)
return self
def hide_now(self):
self._blender_obj.keyframe_insert(data_path="hide_render", frame=FRAME_COUNT)
self._blender_obj.hide_render = True
self._blender_obj.keyframe_insert(data_path="hide_render", frame=FRAME_COUNT+1)
def show_now(self):
self._blender_obj.hide_render = True
self._blender_obj.keyframe_insert(data_path="hide_render", frame=FRAME_COUNT)
self._blender_obj.hide_render = False
self._blender_obj.keyframe_insert(data_path="hide_render", frame=FRAME_COUNT+1)
def keyframe_insert(self, *args, **kwargs):
self._blender_obj.keyframe_insert(*args, **kwargs)
def make_move(board: chess.Board, move: chess.Move, array: List[Optional[CustomPiece]]):
"""
Moves the pieces in blender based on the move from the library.
See EOF for flow chart
"""
locTo = move.to_square
locFrom = move.from_square
print(move, " ", locFrom, " --> ", locTo)
if board.is_castling(move):
if board.turn: # White
if board.is_kingside_castling(move):
array[chess.H1].move(chess.F1)
else: # queen side
array[chess.A1].move(chess.D1)
else: # Black
if board.is_kingside_castling(move):
array[chess.H8].move(chess.F8)
else: # queen side
array[chess.A8].move(chess.D8)
else: # standard case
if board.is_capture(move):# is en passant, great
if board.is_en_passant(move): # normal capture
captured_piece = array[board.ep_square].die() # TODO, do something with this
else: # its a capture,
captured_piece = array[locTo].die() # TODO, do something with this
array[locFrom].move(locTo) # NOTE, piece moves always
if move.promotion is not None:
array[locTo].keyframe_insert(data_path="location", index=-1)
array[locTo].hide_now()
# unlink somehow
pieceType = move.promotion
array[locTo] = CustomPiece(chess.Piece(pieceType, board.turn),\
SOURCE_PIECES[chess.piece_symbol(pieceType)],\
array, locTo)
array[locTo].show_now()
def main(filename) -> Optional[chess.pgn.Game]:
with open(filename) as pgn:
game = chess.pgn.read_game(pgn)
board = game.board()
scene = bpy.context.scene
global SOURCE_PIECES
SOURCE_PIECES = {}
for piece in bpy.data.collections["Pieces"].objects:
# pieces[piece['Square']] = board.piece_at(chess.parse_square(piece['Square']))
SOURCE_PIECES[piece['repr'].lower()] = piece
array = [None for _ in range(64)]
for position in range(64):
if (piece := board.piece_at(position)) is not None:
array[position] = CustomPiece(piece, SOURCE_PIECES[piece.symbol().lower()]\
, array, position)
camera_parent = bpy.data.collections["Collection"].objects['Camera parent']
global FRAME_COUNT
FRAME_COUNT = 0
keyframes(array) # intial pos
FRAME_COUNT += 10
for move in game.mainline_moves():
scene.frame_set(FRAME_COUNT)
make_move(board, move, array)
keyframes(array) # update blender
camera_parent.rotation_euler[2] += radians(2) #XYZ
camera_parent.keyframe_insert(data_path="rotation_euler", index=-1)
board.push(move) # update python-chess
FRAME_COUNT += 10
keyframes(array) # update blender
FRAME_COUNT += 3
confetti = bpy.data.collections["Board"].objects['Confetti source']
if board.outcome() is not None:
winner = board.outcome().winner
king_square = board.king(winner)
xTo, yTo = square_to_world_space(king_square)
confetti.location = Vector((xTo, yTo, 3))
bpy.data.particles["Confetti"].frame_start = FRAME_COUNT
bpy.data.particles["Confetti"].frame_end = FRAME_COUNT + 12
print(FRAME_COUNT)
for _ in range(5):
scene.frame_set(FRAME_COUNT)
camera_parent.rotation_euler[2] += radians(2) #XYZ
camera_parent.keyframe_insert(data_path="rotation_euler", index=-1)
FRAME_COUNT += 13
bpy.data.scenes[0].frame_start = 1
bpy.data.scenes[0].frame_end = FRAME_COUNT - 13
return game
if __name__ == "__main__":
# main("/home/jake/Uni/2nd year/COSC3000/ComputerGraphics/pgn/carlsen_nakamura_2021.pgn")
# main("/home/jake/Uni/2nd year/COSC3000/ComputerGraphics/pgn/testing.pgn")
# main("/home/jake/Uni/2nd year/COSC3000/ComputerGraphics/pgn/Garry Kasparov_vs_Veselin Topalov_1999.pgn")
main("/home/jake/Uni/2nd year/COSC3000/ComputerGraphics/pgn/kramnik_kasparov_2001.pgn")
# main("/home/jake/Uni/2nd year/COSC3000/ComputerGraphics/pgn/grischuk_ponomariov_2000.pgn")