在Mayavi中锁定相机

10

我正尝试在mayavi中使用一系列数据文件制作动画。不幸的是,我注意到相机没有锁定(它会不断缩放)。我认为这是因为我的网格的Z轴分量发生了变化,而mayavi正在尝试重新计算比例尺。

我该如何解决?enter image description hereenter image description hereenter image description here

import numpy
from mayavi import mlab

mlab.figure(size = (1024,768),bgcolor = (1,1,1))
mlab.view(azimuth=45, elevation=60, distance=0.01, focalpoint=(0,0,0))
#mlab.move(forward=23, right=32, up=12)

for i in range(8240,8243):
    n=numpy.arange(10,400,20)
    k=numpy.arange(10,400,20)
    [x,y] = numpy.meshgrid(k,n)
    z=numpy.zeros((20,20))
    z[:] = 5

    M = numpy.loadtxt('B:\\Dropbox\\Master.Diploma\\presentation\\movie\\1disk_j9.5xyz\\'+'{0:05}'.format(i)+'.txt')
    Mx = M[:,0]; My = M[:,1]; Mz = M[:,2]
    Mx = Mx.reshape(20,20); My = My.reshape(20,20); Mz = Mz.reshape(20,20);


    s = mlab.quiver3d(x,y,z,Mx, My, -Mz, mode="cone",resolution=40,scale_factor=0.016,color = (0.8,0.8,0.01))

    Mz = numpy.loadtxt('B:\\Dropbox\\Master.Diploma\\presentation\\movie\\Mzi\\' + '{0:05}'.format(i) + '.txt')
    n=numpy.arange(2.5,400,2)
    k=numpy.arange(2.5,400,2)
    [x,y] = numpy.meshgrid(k,n)

    f = mlab.mesh(x, y, -Mz/1.5,representation = 'wireframe',opacity=0.3,line_width=1)

    mlab.savefig('B:\\Dropbox\\Master.Diploma\\presentation\\movie\\figs\\'+'{0:05}'.format(i)+'.png')
    mlab.clf()
    #mlab.savefig('B:\\Dropbox\\Master.Diploma\\figures\\vortex.png')
    print(i)

mlab.show()
3个回答

6

如果你仍然对此感兴趣,可以尝试在这个上下文中包装你正在进行的任何工作。这将禁用渲染并在上下文退出后将disable_render值和相机视图恢复到原始状态。

with constant_camera_view():
    do_stuff()

这是一个类:
class constant_camera_view(object):
def __init__(self):
    pass

def __enter__(self):
    self.orig_no_render = mlab.gcf().scene.disable_render
    if not self.orig_no_render:
        mlab.gcf().scene.disable_render = True
    cc = mlab.gcf().scene.camera
    self.orig_pos = cc.position
    self.orig_fp = cc.focal_point
    self.orig_view_angle = cc.view_angle
    self.orig_view_up = cc.view_up
    self.orig_clipping_range = cc.clipping_range

def __exit__(self, t, val, trace):
    cc = mlab.gcf().scene.camera
    cc.position = self.orig_pos
    cc.focal_point = self.orig_fp
    cc.view_angle =  self.orig_view_angle 
    cc.view_up = self.orig_view_up
    cc.clipping_range = self.orig_clipping_range

    if not self.orig_no_render:
        mlab.gcf().scene.disable_render = False
    if t != None:
        print t, val, trace
        ipdb.post_mortem(trace)

1
谢谢您的回复 - 知道相机可以在 mlab.gcf().scene.camera 中访问,让我找出如何使用来自Mayavi录制的“实时”相机调整的相机和焦点位置来进行编程设置。 - Dan

4

我没有看出您的情节有什么问题,但要在每个绘图实例后重置视图,请插入您的视点:

mlab.view(azimuth=45, elevation=60, distance=0.01, focalpoint=(0,0,0))

在你的for循环中,在mlab.savefig调用的上方。

1
如果您在网格命令中使用vmin和vmax函数,比例尺就不会随着数据的变化而改变,相机也应该保持原位。示例如下:
f = mlab.mesh(x, y, -Mz/1.5,representation = 'wireframe',vmin='''some value''',vmax='''some value''',opacity=0.3,line_width=1)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接