我正在尝试使用 Mayavi mlab 为地球旋转设置动画。过去,我仅通过围绕地球的 BuiltinSurface 表示旋转相机就成功了,但是当我还需要在框架中绘制许多其他对象(航天器、星星等)时,这会变得不方便。下面的代码似乎“几乎”工作:在我的 Windows 10 机器上,它运行 8 次迭代,然后动画冻结。我该如何修复这段代码,或者有没有更好的方法来为 BuiltinSurface 设置动画?
import numpy as np
from mayavi import mlab
from mayavi.sources.builtin_surface import BuiltinSurface
from mayavi.modules.surface import Surface
from mayavi.filters.transform_data import TransformData
def rotMat3D(axis, angle, tol=1e-12):
"""Return the rotation matrix for 3D rotation by angle `angle` degrees about an
arbitrary axis `axis`.
"""
t = np.radians(angle)
x, y, z = axis
R = (np.cos(t))*np.eye(3) +\
(1-np.cos(t))*np.matrix(((x**2,x*y,x*z),(x*y,y**2,y*z),(z*x,z*y,z**2))) + \
np.sin(t)*np.matrix(((0,-z,y),(z,0,-x),(-y,x,0)))
R[np.abs(R)<tol]=0.0
return R
@mlab.show
@mlab.animate(delay=200)
def anim():
fig = mlab.figure()
engine = mlab.get_engine()
# Add a cylinder builtin source
cylinder_src = BuiltinSurface()
engine.add_source(cylinder_src)
cylinder_src.source = 'earth'
# Add transformation filter to rotate cylinder about an axis
transform_data_filter = TransformData()
engine.add_filter(transform_data_filter, cylinder_src)
Rt = np.eye(4)
Rt[0:3,0:3] = rotMat3D((0,0,1), 0) # in homogeneous coordinates
Rtl = list(Rt.flatten()) # transform the rotation matrix into a list
transform_data_filter.transform.matrix.__setstate__({'elements': Rtl})
transform_data_filter.widget.set_transform(transform_data_filter.transform)
transform_data_filter.filter.update()
transform_data_filter.widget.enabled = False # disable the rotation control further.
# Add surface module to the cylinder source
cyl_surface = Surface()
engine.add_filter(cyl_surface, transform_data_filter)
#add color property
#cyl_surface.actor.property.color = (1.0, 0.0, 0.0)
ind=1
while ind<90:
print ind
Rt[0:3,0:3] = rotMat3D((0,0,1), ind) # in homogeneous coordinates
Rtl = list(Rt.flatten()) # transform the rotation matrix into a list
transform_data_filter.transform.matrix.__setstate__({'elements': Rtl})
transform_data_filter.widget.set_transform(transform_data_filter.transform)
transform_data_filter.filter.update()
transform_data_filter.widget.enabled = False # disable the rotation control further.
# Add surface module to the cylinder source
cyl_surface = Surface()
engine.add_filter(cyl_surface, transform_data_filter)
# add color property
#cyl_surface.actor.property.color = (1.0, 0.0, 0.0)
yield
ind+=1
anim()