Mayavi:在不阻塞的情况下动画八面体轮廓



我正在尝试动画一个八面体。这里是代码。这个代码的一个更简单的版本可以在SO上的这个不同的问题上找到。使用这里的代码和动画对象的样式,我尝试制作一个功能动画。唯一的问题是-动画挂起情节窗口!

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

我不觉得剧情吊人胃口。只是dt太小了,而time.sleep太大了,很考验你的耐心。如果将dt设置为0.1,并删除time.sleep调用,则情节变得更加生动。

另外,当数组的大小改变时,使用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()

相关内容

  • 没有找到相关文章

最新更新