注册一个在Visualizer更新场景时周期性调用的动画回调函数。
Visualizer.register_animation_callback(animation_callback: AnimationCallback) -> None
animation_callback
(AnimationCallback
):要注册的动画回调函数。该函数将周期性地被调用并得到Visualizer当前的状态。该方法没有返回值。
以下示例演示如何使用register_animation_callback方法,通过添加动画回调,实现场景的周期性刷新。
首先,我们创建一个简单的场景,包括一个矩形和一个球体,并将其可视化:
import open3d.visualization as vis
import numpy as np
vis.initialize_visualizer()
vis.create_window()
mesh_box = vis.geometry.create_mesh_box(width=1.0, height=1.0, depth=1.0)
mesh_sphere = vis.geometry.create_mesh_sphere(radius=0.5)
mesh_box.paint_uniform_color([0.9, 0.1, 0.1])
mesh_sphere.paint_uniform_color([0.1, 0.9, 0.1])
vis.add_geometry(mesh_box)
vis.add_geometry(mesh_sphere)
vis.reset_camera()
vis.run()
然后,我们定义一个动画回调函数animation_callback,实现对矩形和球体的周期性变换:
def animation_callback(vis: vis.Visualizer):
# 获取矩形和球体的变换矩阵
trans_box = vis.get_geometry_matrix(mesh_box)
trans_sphere = vis.get_geometry_matrix(mesh_sphere)
# 每帧矩形沿x轴旋转1度
rotx = np.asarray([[1, 0, 0], [0, np.cos(np.pi/180), -np.sin(np.pi/180)], [0, np.sin(np.pi/180), np.cos(np.pi/180)]])
trans_box[:3,:3] = rotx.dot(trans_box[:3,:3])
# 每隔50帧,球体沿y轴旋转15度
if vis.get_iteration_number() % 50 == 0:
roty = np.asarray([[np.cos(np.pi/12), 0, np.sin(np.pi/12)], [0, 1, 0], [-np.sin(np.pi/12), 0, np.cos(np.pi/12)]])
trans_sphere[:3,:3] = roty.dot(trans_sphere[:3,:3])
# 将变换矩阵应用到物体上
vis.update_geometry(mesh_box, trans_box)
vis.update_geometry(mesh_sphere, trans_sphere)
最后,我们将动画回调函数注册到Visualizer中,并启动场景的更新循环:
vis.register_animation_callback(animation_callback)
vis.run()
现在,我们就可以看到一个周期性变换的场景了。您可以通过修改动画回调函数animation_callback,实现更多有趣的动画效果。