我正在尝试为八面体制作动画。这是代码。将找到此代码的更简单版本on this different question on SO。使用那里的代码和使用的动画对象的样式here我尝试制作一个功能性动画。唯一的问题是 - 动画挂起了情节窗口!
import numpy as np
import mayavi.mlab as ML
import math
import time
def produce_verts(A,t):
delta=lambda A,t:A*math.sin(t)
verts =lambda d: [(1+d,0,0), (0,1+d,0), (-1-d,0,0), (0,-1-d,0), (0,0,1+d), (0,0,-1-d)]
return zip(*verts(delta(A,t)))
t=0.
dt=0.01
A=0.5
ML.clf()
nverts=6
x, y, z = produce_verts(A,t)
# Each triangle is a 3-tuple of indices. The indices are indices of `verts`.
triangles = [(i, (i+1)%4, j) for i in range(4) for j in (4,5)]
colorval = [x[i]**2+y[i]**2+z[i]**2 for i in range(nverts)]
mesh=ML.triangular_mesh(x, y, z, triangles, scalars=colorval, opacity=1,representation='mesh')
MS=mesh.mlab_source
Bool=True
while Bool:
t=(t+dt)%(2*math.pi)
x,y,z=produce_verts(A,t)
colorval = [x[i]**2+y[i]**2+z[i]**2 for i in range(nverts)]
MS.reset(x=x,y=y,z=z,scalars=colorval)
time.sleep(1.)
print t,dt
if t>4:
Bool=False
答案 0 :(得分:1)
我不认为情节悬而未决。只是dt
非常小而time.sleep
非常大,以至于它会让你耐心等待。如果您将dt
设置为等于,例如0.1,并删除time.sleep
调用,那么该绘图将变得更加生动。
此外,当阵列的 size 发生变化时,请使用MS.reset
。当阵列的大小保持不变时,使用MS.set
可以获得更好的性能:
import numpy as np
import mayavi.mlab as ML
import math
import time
def produce_verts(A, t):
def delta(A, t):
return A * math.sin(t)
def verts(d):
return [(1 + d, 0, 0), (0, 1 + d, 0), (-1 - d, 0, 0), (0, -1 - d, 0),
(0, 0, 1 + d), (0, 0, -1 - d)]
return zip(*verts(delta(A, t)))
t = 0.
dt = 0.1
A = 0.5
ML.clf()
nverts = 6
x, y, z = produce_verts(A, t)
# Each triangle is a 3-tuple of indices. The indices are indices of `verts`.
triangles = [(i, (i + 1) % 4, j) for i in range(4) for j in (4, 5)]
colorval = [xi ** 2 + yi ** 2 + zi ** 2 for xi, yi, zi in zip(x, y, z)]
mesh = ML.triangular_mesh(
x, y, z, triangles, scalars=colorval, opacity=1, representation='mesh')
ms = mesh.mlab_source
while True:
t = (t + dt) % (2 * math.pi)
x, y, z = produce_verts(A, t)
colorval = [xi ** 2 + yi ** 2 + zi ** 2 for xi, yi, zi in zip(x, y, z)]
ms.set(x=x, y=y, z=z, scalars=colorval)
# time.sleep(0.1)
print t, dt
if t > 4:
break
ML.show()