Replies: 3 comments
-
The better (and correct) way would probably to create a new object (based on Above all you should always call the |
Beta Was this translation helpful? Give feedback.
-
I wanted to add to this to agree. In software, it is best to consider composition over inheritance. Does it makes sense to say that a " Make an object, that encapsulates what a Edit: In this case, it may make sense to say that a |
Beta Was this translation helpful? Give feedback.
-
Thank you both, immensely helpful comments. I did consider using composition over inheritance, you're correct in that it may be the way to go. Thanks again. [Updated with code] Still a little ugly as replacing a cell's content seems awkward having to store outside the Text MObject coordinates and actual text to be able to replace them efficiently. The use of float, (presumably instead of double) means that there are minor changes to the X, and Y coordinates that using set_x(mobject.get(x)) actually ends up a little off center. Matrix was also ugle for the desired "look"... class Cell:
def __init__(self, cellContent = "", cellBorderWidth = 0.1, \
cellBorderColor = WHITE, textColor = WHITE, cellBorderScale = 0.333):
self.Border = Square(stroke_width = cellBorderWidth, color = cellBorderColor).scale(cellBorderScale)
self.Scene = VGroup(self.Border)
self.Content = Text(cellContent, color = textColor)
self.Scene += self.Content
self.Text = cellContent
self.X = 0
self.Y = 0
def SetContent(self, Scene, cellContent, run_time = 0.1, textColor = WHITE):
if self.Text == cellContent:
return
if self.Text != "":
Scene.play(Unwrite(self.Content, reverse = False, run_time = run_time))
self.Scene -= self.Content
self.Text = cellContent
text = Text(cellContent, color = textColor)
text.set_x(self.X)
text.set_y(self.Y)
self.Content = text
Scene.play(Write(self.Content, run_time = run_time))
self.Scene += self.Content
class Box:
def __init__(self, boxBorderWidth = 5, cellBorderWidth = 0.1, \
boxBorderColor = WHITE, cellBorderColor = WHITE, cellTextColor = WHITE):
self.Border = Square(stroke_width = boxBorderWidth, color = boxBorderColor)
self.Scene = VGroup(self.Border)
self.Cells = []
for i in range(0, 3):
for j in range(0, 3):
x = i * 3 + j
cell = Cell(cellBorderWidth = cellBorderWidth, \
cellBorderColor = boxBorderColor, \
textColor = cellTextColor)
cell.Scene.shift([(j - 1) * 2/3, (i - 1) * 2/3, 0])
cell.X = (j - 1) * 2/3
cell.Y = (i - 1) * 2/3
self.Cells.append(cell)
self.Scene += cell.Scene
def Shift(self, shiftVector):
self.Scene.shift(shiftVector)
for i in range(0, 3):
for j in range(0, 3):
x = i * 3 + j
self.Cells[x].X += shiftVector[0]
self.Cells[x].Y += shiftVector[1]
class SudokuGrid:
def __init__(self, gridBorderWidth = 10, gridBorderColor = WHITE, \
boxBorderWidth = 5, boxBorderColor = WHITE, \
cellBorderWidth = 0.7, cellBorderColor = WHITE, cellTextColor = WHITE):
self.Border = Square(stroke_width = gridBorderWidth, color = gridBorderColor).scale(3)
self.Scene = VGroup(self.Border)
self.Boxes = []
for i in range(0, 3):
for j in range(0, 3):
box = Box(boxBorderWidth = boxBorderWidth, \
boxBorderColor = boxBorderColor, \
cellBorderWidth = cellBorderWidth, \
cellBorderColor = cellBorderColor, \
cellTextColor = cellTextColor)
box.Shift([(j - 1) * 2, (i - 1) * 2, 0])
self.Boxes.append(box)
self.Scene += box.Scene |
Beta Was this translation helpful? Give feedback.
-
Description of bug / unexpected behavior
I am trying to create a Sudoku grid class in Manim. If you're unfamiliar with Sudoku grids, they're a grid of boxes (9 of them), and each of them are a grid of cells (9 of them again). A perfect opportunity for OO if there ever was one.
So I have three classes,
Which necessitates overriding
__init__
to have member variables in their classes.And whenever you override
__init__
manim just falls apart with the error in the subject line.No attempt by me to move subsets of the code between
__init__
andconstruct
has allowed me to overcome this issue.See below for sample code and how to reproduce the issue.
Expected behavior
This may be my lack of imagination, but I expect to be able to create OO objects that allow me to abstract complexity rather than have to manage all 91 boxes of a Sudoku grid individually every time I wish to animate one. Grouping them into a vgroup works of course if you only have one grid, but how do I then address the cells and populate their text or add more complex animations?
How to reproduce the issue
Code for reproducing the problem
Logs
Terminal output
System specifications
System Details
python/py/python3 --version
): Python 3.11.4pip list
):FFMPEG
Output of
ffmpeg -version
:Beta Was this translation helpful? Give feedback.
All reactions