【问题标题】:Mayavi: animating an octahedron outline without blockingMayavi:在不阻塞的情况下为八面体轮廓设置动画
【发布时间】:2014-07-25 15:37:26
【问题描述】:

我正在尝试为八面体设置动画。这是代码。可以在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

【问题讨论】:

    标签: python animation mayavi


    【解决方案1】:

    我不认为情节悬而未决。只是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()
    

    【讨论】:

      猜你喜欢
      • 2019-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多