简体   繁体   English

Python:在循环中连续添加到3d场景

[英]Python: Continuous addition to a 3d scene in a loop

Can you give example code to 你能给示例代码

  1. initially draw a surface 最初绘制表面
  2. then draw some random lines in a loop. 然后在循环中绘制一些随机线。 Between drawing each line, a little time is wasted, to give the impression of an animation. 在绘制每条线之间,浪费了一些时间,给人以动画的印象。

The code should work both from ipython and pydev . 该代码应该可以在ipythonpydev

One answer using the mayavi is below: 使用mayavi一个答案如下:

import numpy as np
from mayavi import mlab
import time
from tvtk.tools import visual


# # prepare surface data
rng = 20
step = 1
X = np.arange(0, rng, step)
Y = X
Z = np.random.uniform(-1, 0, (rng, rng))

# # draw the surface
fig = mlab.figure(size=(500, 500), bgcolor=(1, 1, 1))
visual.set_viewer(fig)
s = mlab.surf(X, Y, Z)
mlab.axes(color=(0, 0, 0))
mlab.view(40, 40)
mlab.outline()

for i in xrange(5):
    # # sleep a little to give the impression of animation
    time.sleep(1)
    # # get coordinates of two random points
    p1 = np.random.uniform(0, 10, (3,)) 
    p2 = np.random.uniform(0, 10, (3,))
    line = np.vstack((p1, p2))
    x, y, z = (line[:, dim] for dim in xrange(3))

    # # connect points
    mlab.plot3d(x, y, z, figure=fig, tube_radius=.05, colormap='Greens')

mlab.show()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM