MacOS – How to install mayAVI for the python3 on macOS Sierra

macospython

I have been using matplotlib to plot 3D graphs, however I have found that matplotlib does not have a drag and rotate function for the plotted 3D graphs. I would like to install mayAVI that will enable me to do so, however I can't find any foolproof methods to install it. Could anyone guide me in the installation process? I do not use virtual environments.

Best Answer

I recently experienced your exact same problem (wanting to use MayaVi instead of matplotlib for 3D graphs and animations, not wanting to use virtual environments, and wanting to use MayaVi with python3). These are the direct steps I followed to start using MayaVi on my macOS Sierra 10.12.5.

(1) brew install vtk --with-python3 --without-python

  • This took roughly 33 minutes on my machine
  • From what I have read, there can only be one version/wrapper of vtk installed at a time, so since you installed vtk without the flags, you might want to run brew uninstall vtk before running this command

(2) pip3 install mayavi

Next I tried running the script from http://docs.enthought.com/mayavi/mayavi/auto/example_mlab_visual.html#example-mlab-visual to see if it would run. I took that script exactly as found, added #!/usr/bin/env python3 and ran it as ./test.py. This resulted in:

======

Traceback (most recent call last): File "./test.py", line 6, in f = mlab.figure(size=(500,500)) File "/usr/local/lib/python3.6/site-packages/mayavi/tools/figure.py", line 63, in figure engine = get_engine() File "/usr/local/lib/python3.6/site-packages/mayavi/tools/engine_manager.py", line 101, in get_engine return self.new_engine() File "/usr/local/lib/python3.6/site-packages/mayavi/tools/engine_manager.py", line 146, in new_engine check_backend() File "/usr/local/lib/python3.6/site-packages/mayavi/tools/engine_manager.py", line 49, in check_backend ''') ImportError: Could not import backend for traits


Make sure that you have either the TraitsBackendWx or the TraitsBackendQt projects installed. If you installed Mayavi with easy_install, try easy_install . easy_install Mayavi[app] will also work.

If you performed a source checkout, be sure to run 'python setup.py install' in Traits, TraitsGUI, and the Traits backend of your choice.

Also make sure that either wxPython or PyQT is installed. wxPython: http: //www.wxpython.org/ PyQT: http: //www.riverbankcomputing.co.uk/software/pyqt/intro`

=====

So after googling the above problem I came across a github issue page that said to install the following items:

(3) brew install wxpython

(4) pip3 install traitsui (this was already installed on my machine)

Next, needing to install PyQt, so I followed the directions from a stack overflow question:

(5) brew tap cartr/qt4

(6) brew tap-pin cartr/qt4

(7) brew install qt

(8) brew install pyside

  • This took about 11 minutes on my machine

Now I tried running the script again. This time it ran, but I got the error:

AttributeError: module 'tvtk.tools.visual' has no attribute 'color'

So I changed the import line and modified some of the lines where the boxes are made:

#!/usr/bin/env python3                                                                               

from mayavi import mlab
from tvtk.tools import visual
from vtk.util import colors as color

# Create a figure                                                                                    
f = mlab.figure(size=(500,500))
# Tell visual to use this as the viewer.                                         
visual.set_viewer(f)

# A silly visualization.                                                                             
mlab.test_plot3d()

# Even sillier animation.                                                                            
b1 = visual.box()
b2 = visual.box(x=4., color=color.red)
b3 = visual.box(x=-4, color=color.red)
b1.v = 5.0

@mlab.show
@mlab.animate(delay=250)
def anim():
    """Animate the b1 box."""
    while 1:
        b1.x = b1.x + b1.v*0.1
        if b1.x > 2.5 or b1.x < -2.5:
            b1.v = -b1.v
        yield

# Run the animation.                                                                                 
anim()

And I can successfully run the script!