简体   繁体   English

如何在 manimce 中一个接一个地应用两个转换?

[英]How to apply two tranformations one after the other in manimce?

I wanted to apply two linear transformations with matrices one after the other in manimce .我想在manimce一个接一个地应用两个带有矩阵的线性变换 Here is the code for one transformation:这是一种转换的代码:

 from manim import *

class LT(LinearTransformationScene):
def __init__(self):
    LinearTransformationScene.__init__(
        self,
        show_coordinates=True,
        leave_ghost_vectors=True,
    )

def construct(self):
    matrix = [[1, 1], [0, 1]]
    self.apply_matrix(matrix)
    self.wait()

This works perfectly for a single transformation.这对于单个转换非常有效。 But I want to apply another transformation after this.但我想在此之后应用另一个转换。 I tried using self.apply_matrix again:我再次尝试使用self.apply_matrix

from manim import *

class LT(LinearTransformationScene):
    def __init__(self):
        LinearTransformationScene.__init__(
            self,
            show_coordinates=True,
            leave_ghost_vectors=True,
        )

    def construct(self):
        matrix1 = [[1, 1], [0, 1]]
        self.apply_matrix(matrix1)
        self.wait()


    def construct(self):
        matrix2 = [[2, 1], [3, 1]]
        self.apply_matrix(matrix2)
        self.wait()

with this code, only the last matrix was rendered.使用此代码,只渲染了最后一个矩阵。 What might have happened?可能发生了什么?

This must be very simple.这一定非常简单。 But I have no idea- didnt found anything in the documentation or search results.Any help is greatly appreciated.但我不知道 - 没有在文档或搜索结果中找到任何内容。非常感谢任何帮助。


Update:更新:

As suggested by Nils Werner, I changed LinearTransformationScene.__init__(...) to super().__init__(...) and included the two matrices in the same def construct(self): .正如 Nils Werner 所建议的那样,我将LinearTransformationScene.__init__(...)更改为super().__init__(...)并将这两个矩阵包含在同一个def construct(self):

from manim import *

class LT(LinearTransformationScene):
    def __init__(self):
        super().__init__(
            self,
            show_coordinates=True,
            leave_ghost_vectors=True,
        )

def construct(self):
    matrix1 = [[1, 1], [0, 1]]
    self.apply_matrix(matrix1)

    matrix2 = [[2, 1], [3, 1]]
    self.apply_matrix(matrix2)

    self.wait()

But this didn't work either.但这也不起作用。 Shows error:显示错误:

ValueError: operands could not be broadcast together with shapes (4,3) (12,3)

Full log:完整日志:

 PS C:\\Python38> manim LTmul.py -pql Manim Community v0.11.0 [10/26/21 21:24:39] INFO Animation 0 : Using cached data (hash : cairo_renderer.py:110 3163782288_2165879958_531335182) ┌─────────────────────────────── Traceback (most recent call last) ────────────────────────────────┐ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\cli\\render\\commands.py:139 in render │ │ │ │ 136 │ │ for SceneClass in scene_classes_from_file(file): │ │ 137 │ │ │ try: │ │ 138 │ │ │ │ scene = SceneClass() │ │ > 139 │ │ │ │ scene.render() │ │ 140 │ │ │ except Exception: │ │ 141 │ │ │ │ error_console.print_exception() │ │ 142 │ │ │ │ sys.exit(1) │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\scene\\scene.py:213 in render │ │ │ │ 210 │ │ """ │ │ 211 │ │ self.setup() │ │ 212 │ │ try: │ │ > 213 │ │ │ self.construct() │ │ 214 │ │ except EndSceneEarlyException: │ │ 215 │ │ │ pass │ │ 216 │ │ except RerunSceneException as e: │ │ │ │ C:\\Python38\\LTmul.py:16 in construct │ │ │ │ 13 │ │ self.apply_matrix(matrix1) │ │ 14 │ │ │ │ 15 │ │ matrix2 = [[2, 2], [3, 3]] │ │ > 16 │ │ self.apply_matrix(matrix2) │ │ 17 │ │ │ │ 18 │ │ self.wait() │ │ 19 │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\scene\\vector_space_scene.py:1041 in apply_matrix │ │ │ │ 1038 │ │ **kwargs │ │ 1039 │ │ │ Any valid keyword argument of self.apply_transposed_matrix() │ │ 1040 │ │ """ │ │ > 1041 │ │ self.apply_transposed_matrix(np.array(matrix).T, **kwargs) │ │ 1042 │ │ │ 1043 │ def apply_inverse(self, matrix, **kwargs): │ │ 1044 │ │ """ │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\scene\\vector_space_scene.py:1077 in │ │ apply_transposed_matrix │ │ │ │ 1074 │ │ │ │ [angle_of_vector(func(RIGHT)), angle_of_vector(func(UP)) - np.pi / 2], │ │ 1075 │ │ │ ) │ │ 1076 │ │ │ kwargs["path_arc"] = net_rotation │ │ > 1077 │ │ self.apply_function(func, **kwargs) │ │ 1078 │ │ │ 1079 │ def apply_inverse_transpose(self, t_matrix, **kwargs): │ │ 1080 │ │ """ │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\scene\\vector_space_scene.py:1145 in apply_function │ │ │ │ 1142 │ │ │ + [Animation(f_mob) for f_mob in self.foreground_mobjects] │ │ 1143 │ │ │ + added_anims │ │ 1144 │ │ ) │ │ > 1145 │ │ self.play(*anims, **kwargs) │ │ 1146 │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\scene\\scene.py:888 in play │ │ │ │ 885 │ │ │ return np.max([animation.run_time for animation in animations]) │ │ 886 │ │ │ 887 │ def play(self, *args, **kwargs): │ │ > 888 │ │ self.renderer.play(self, *args, **kwargs) │ │ 889 │ │ │ 890 │ def wait(self, duration=DEFAULT_WAIT_TIME, stop_condition=None): │ │ 891 │ │ self.play(Wait(run_time=duration, stop_condition=stop_condition)) │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\renderer\\cairo_renderer.py:127 in play │ │ │ │ 124 │ │ self.static_image = self.save_static_frame_data(scene, scene.static_mobjects) │ │ 125 │ │ │ │ 126 │ │ self.file_writer.begin_animation(not self.skip_animations) │ │ > 127 │ │ scene.begin_animations() │ │ 128 │ │ if scene.is_current_animation_frozen_frame(): │ │ 129 │ │ │ self.update_frame(scene) │ │ 130 │ │ │ # self.duration stands for the total run time of all the animations. │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\scene\\scene.py:961 in begin_animations │ │ │ │ 958 │ def begin_animations(self) -> None: │ │ 959 │ │ """Start the animations of the scene.""" │ │ 960 │ │ for animation in self.animations: │ │ > 961 │ │ │ animation.begin() │ │ 962 │ │ │ 963 │ def is_current_animation_frozen_frame(self) -> bool: │ │ 964 │ │ """Returns whether the current animation produces a static frame (generally a Wa │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\animation\\transform.py:114 in begin │ │ │ │ 111 │ │ │ self.mobject.align_data_and_family(self.target_copy) │ │ 112 │ │ else: │ │ 113 │ │ │ self.mobject.align_data(self.target_copy) │ │ > 114 │ │ super().begin() │ │ 115 │ │ │ 116 │ def create_target(self) -> Mobject: │ │ 117 │ │ # Has no meaningful effect here, but may be useful │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\animation\\animation.py:184 in begin │ │ │ │ 181 │ │ │ # the internal updaters of self.starting_mobject, │ │ 182 │ │ │ # or any others among self.get_all_mobjects() │ │ 183 │ │ │ self.mobject.suspend_updating() │ │ > 184 │ │ self.interpolate(0) │ │ 185 │ │ │ 186 │ def finish(self) -> None: │ │ 187 │ │ # TODO: begin and finish should require a scene as parameter. │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\animation\\animation.py:284 in interpolate │ │ │ │ 281 │ │ │ The relative time to set the aniamtion to, 0 meaning the start, 1 meaning │ │ 282 │ │ │ the end. │ │ 283 │ │ """ │ │ > 284 │ │ self.interpolate_mobject(alpha) │ │ 285 │ │ │ 286 │ def interpolate_mobject(self, alpha: float) -> None: │ │ 287 │ │ """Interpolates the mobject of the :class:`Animation` based on alpha value. │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\animation\\animation.py:299 in interpolate_mobject │ │ │ │ 296 │ │ families = list(self.get_all_families_zipped()) │ │ 297 │ │ for i, mobs in enumerate(families): │ │ 298 │ │ │ sub_alpha = self.get_sub_alpha(alpha, i, len(families)) │ │ > 299 │ │ │ self.interpolate_submobject(*mobs, sub_alpha) │ │ 300 │ │ │ 301 │ def interpolate_submobject( │ │ 302 │ │ self, │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\animation\\transform.py:152 in interpolate_submobject │ │ │ │ 149 │ │ target_copy: Mobject, │ │ 150 │ │ alpha: float, │ │ 151 │ ) -> "Transform": │ │ > 152 │ │ submobject.interpolate(starting_submobject, target_copy, alpha, self.path_func) │ │ 153 │ │ return self │ │ 154 │ │ 155 │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\mobject\\mobject.py:2597 in interpolate │ │ │ │ 2594 │ │ │ │ │ │ │ 2595 │ │ │ │ │ self.add(dotL, dotR, dotMiddle) │ │ 2596 │ │ """ │ │ > 2597 │ │ self.points = path_func(mobject1.points, mobject2.points, alpha) │ │ 2598 │ │ self.interpolate_color(mobject1, mobject2, alpha) │ │ 2599 │ │ return self │ │ 2600 │ │ │ │ C:\\tools\\Manim\\Lib\\site-packages\\manim\\utils\\paths.py:43 in path │ │ │ │ 40 │ │ │ 41 │ def path(start_points, end_points, alpha): │ │ 42 │ │ if arc_centers is None: │ │ > 43 │ │ │ vects = end_points - start_points │ │ 44 │ │ │ centers = start_points + 0.5 * vects │ │ 45 │ │ │ if arc_angle != np.pi: │ │ 46 │ │ │ │ centers += np.cross(unit_axis, vects / 2.0) / np.tan(arc_angle / 2) │ └──────────────────────────────────────────────────────────────────────────────────────────────────┘ ValueError: operands could not be broadcast together with shapes (4,3) (12,3)

Apply the two matrices in the same construct() call.在同一个construct()调用中应用两个矩阵。

from manim import *

class LT(LinearTransformationScene):
    def __init__(self):
        super().__init__(
            self,
            show_coordinates=True,
            leave_ghost_vectors=True,
        )

    def construct(self):
        matrix1 = [[1, 1], [0, 1]]
        self.apply_matrix(matrix1)

        matrix2 = [[2, 1], [3, 1]]
        self.apply_matrix(matrix2)

        self.wait()

I just needed to add a self.wait() between the matrices!!我只需要在矩阵之间添加一个self.wait() !!

Thanks Nils Werner for suggesting this and to add the matrices in the same construct() call.感谢 Nils Werner 提出这个建议并在同一个construct()调用中添加矩阵。

from manim import *

class LinearTransformationSceneExample(LinearTransformationScene):
    def __init__(self):
        LinearTransformationScene.__init__(
            self,
            show_coordinates=True,
            leave_ghost_vectors=True,
    )

def construct(self):
    matrix = [[1, 1], [0, 1]]
    self.apply_matrix(matrix)
    self.wait()

    matrix2 = [[2, 1], [3, 1]]
    self.apply_matrix(matrix2)

    self.wait()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Coconut 中一个接一个地实现两种方法 - How to implement two methods one after the other in Coconut 如何使用python考虑其他两个变量对一个变量应用过滤器 - how to apply filter on one variable considering other two variable using python 如何让selenium一个接一个地执行两个不同的按钮 - How to make selenium execute two different buttons one after the other 如何将两个模型应用于一个视图? - How to apply two models to one view? 如何将操作应用于list_filter中的多个项目,而不必一个接一个地单独执行? - How can I apply an action to multiple items in a list_filter, without having to do them individually, one after the other? 在jupyter笔记本中一个接一个地运行两个file.py - run two file.py one after the other in jupyter notebook Kivy:绑定两个方法,一个接一个地调用 - Kivy: bind two methods to be called one after the other 如果满足条件,如何将两种不同的功能应用于一列? - How to apply two different functions to one column if meets the condition? 关闭另一个窗口后如何重新打开? - How to reopen window after closing other one? 如何一个接一个地打印字母表并扩展它 - How to print the alphabet one after the other and extend this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM