Here is an minimal code to visualize 3d box animation using mayavi
and tvtk
.
At time t==20
, I tried to add a new green box.
However, the visual.box
automatically run reset_zoom
inside, and suddenly camera's viewpoint is changed.
How can I add/remove boxes at any time, without affecting the camera?
Is there a better way than using tvtk
?
from mayavi import mlab
from tvtk.tools import visual
fig = mlab.figure(size=(500,500))
visual.set_viewer(fig)
b1 = visual.box()
b2 = visual.box(x=4)
b3 = visual.box(x=-4)
b1.v = 5.0
mlab.view(azimuth=45, elevation=70, focalpoint=[0, 0, 0], distance=62.0, figure=fig)
@mlab.show
@mlab.animate(delay=100)
def anim():
for i in range(30):
b1.x = b1.x + b1.v*0.1
if b1.x > 2.5 or b1.x < -2.5:
b1.v = -b1.v
if i==20:
""" HERE adding a box automatically reset zooming """
newbox = visual.box(x=2, y=2, z=2, color=(0,1,0))
yield
anim()