Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

locking camera in mayavi

Tags:

python

mayavi

I'm trying to make an animation with a sequence of datafiles in mayavi. Unfortunately i have noticed that camera doesn't lock (it is zooming and zooming out). I think it is happening because the Z componrnt of my mesh is changing and mayavi is trying to recalculate scales.

How can I fix it? 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()
like image 996
anatoly Avatar asked Jun 12 '13 10:06

anatoly


2 Answers

for anyone still interested in this, you could try wrapping whatever work you're doing in this context, which will disable rendering and return the disable_render value and camera views to their original states after the context exits.

with constant_camera_view():
    do_stuff()

Here's the class:

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)
like image 81
eqzx Avatar answered Nov 12 '22 16:11

eqzx


I do not really see the problem in your plot but to reset the view after each plotting instance insert your view point:

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

directly above your mlab.savefig callwithin your for loop .

like image 40
Andre Scholich Avatar answered Nov 12 '22 16:11

Andre Scholich