hotkeys for Mayavi scene

107 views
Skip to first unread message

Darren Williams

unread,
May 4, 2020, 4:33:50 PM5/4/20
to Enthought Tool Suite users
I would like to add a hotkey connected to an arbitrary function in a 
Mayavi Scene dialogue.. This may require editing the following 
function from the scene.py program, but I'd like to reach out for 
some advice before I start messing with it. It seems straightforward 
how to make additional hotkeys in the code, but how to attach them 
to a callable function is not clear to me. 
Thanks in advance.

def keyPressEvent(self, e):
""" This method is overridden to prevent the 's'/'w'/'e'/'q' keys from
doing the default thing which is generally useless. It also handles
the 'p' and 'l' keys so the picker and light manager are called.
"""

Prabhu Ramachandran

unread,
May 6, 2020, 11:45:25 AM5/6/20
to Darren Williams, Enthought Tool Suite users
Hi Darren,

I have for a long while wanted to abstract out the keyboard actions and make them pluggable but never got around to it.  I am afraid that if you want to change these, you will need to override the methods.  You can subclass the scene and change the methods. Then you can in principle monkey patch the required classes into tvtk.pyface.  

I think a cleaner way would be to use the pyface.toolkits that tvtk contributes.  See here: https://docs.enthought.com/pyface/toolkits.html
If you see mayavi's setup.py it basically adds these contributions:

'tvtk.toolkits': [
                'qt4 = tvtk.pyface.ui.qt4.init:toolkit_object',
                'qt = tvtk.pyface.ui.qt4.init:toolkit_object',
                'wx = tvtk.pyface.ui.wx.init:toolkit_object',
                'null = tvtk.pyface.ui.null.init:toolkit_object',
            ]

I think in principle you should be able to override these with your own toolkit object which uses your  subclass.  I haven't tried this myself but I believe it should work in principle.

HTH.

cheers,
Prabhu


--
You received this message because you are subscribed to the Google Groups "Enthought Tool Suite users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ets-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ets-users/1f6074e9-0c27-4201-a064-d0b2ed9a8b5b%40googlegroups.com.

Williams, Darren

unread,
Jul 20, 2021, 12:04:12 PM7/20/21
to Prabhu Ramachandran, Enthought Tool Suite users
Hi Everyone: 

I'm running Mayavi 4.7.2 and Traits 6.2 on Windows 10.
All was working with previous installs. But now every
time I attempt to run mlab,

from mayavi import mlab

 it hangs in the traits.py routine. 

Line 39 
 from .constants import (
ImportError: attempted relative import with no known parent package

I've tried removing the . in front of the constants and then it throws another 
error in the constants.py program stating
 line 13, in <module> import traits.ctrait

ModuleNotFoundError: No module named 'traits.ctrait'; 'traits' is not a package

and I've checked my directories, and traits.ctrait is​ a package. 

Please advise. 

Thanks.

Darren Williams


From: Prabhu Ramachandran <pra...@enthought.com>
Sent: Wednesday, May 6, 2020 11:45 AM
To: Williams, Darren <dmw...@psu.edu>
Cc: Enthought Tool Suite users <ets-...@googlegroups.com>
Subject: Re: [ets-users] hotkeys for Mayavi scene
 

Mark Dickinson

unread,
Jul 20, 2021, 2:27:03 PM7/20/21
to Williams, Darren, Prabhu Ramachandran, Enthought Tool Suite users
The errors you're seeing are the ones I'd expect to see if the traits package directory were on the Python path. That's not generally something that you want to happen.

Some diagnostic steps you can take:

- Check that your current working directory isn't somewhere in the Traits tree
- If you're executing a Python script, check that that script isn't somewhere in the Traits tree
- From a clean directory, try starting Python and doing "import traits.api". Does this succeed? If not, what error is printed?
- If the last step fails, try an "import sys" followed by "print(sys.path)". Does the printed list include the traits directory?
- If the traits directory _is_ in the path printed in the last step, check the value of the PYTHONPATH environment variable. Is the traits directory in that?

Mark
_______________

Mark Dickinson, PhD
Director, Software Architecture | Enthought



Williams, Darren

unread,
Jul 20, 2021, 2:58:47 PM7/20/21
to Mark Dickinson, Prabhu Ramachandran, Enthought Tool Suite users
Hi Mark. 

import traits.api trips the same error with the .constants code 

sys.path includes the traits directory

also, I checked the PYTHONPATH in my Windows10 Environment 
and the traits directory is included in there

I also compiled my python code in some other directory outside the 
Python directories.

Hmmm.

Darren

From: Mark Dickinson <mdick...@enthought.com>
Sent: Tuesday, July 20, 2021 2:26 PM
To: Williams, Darren <dmw...@psu.edu>
Cc: Prabhu Ramachandran <pra...@enthought.com>; Enthought Tool Suite users <ets-...@googlegroups.com>

Mark Dickinson

unread,
Jul 21, 2021, 2:27:23 AM7/21/21
to Williams, Darren, Enthought Tool Suite users
Hi Darren,

also, I checked the PYTHONPATH in my Windows10 Environment 
and the traits directory is included in there

Okay, that would do it. The quick fix is of course to remove that directory from your PYTHONPATH. The harder problem to solve is figuring out how it got there in the first place, and how to stop that happening again. I'm not sure we can help you much with that one.

Mark
_______________

Mark Dickinson, PhD
Director, Software Architecture | Enthought

Jonathan March

unread,
Jul 21, 2021, 9:11:14 AM7/21/21
to Enthought Tool Suite users

Williams, Darren

unread,
Jul 21, 2021, 11:21:21 AM7/21/21
to Jonathan March, Enthought Tool Suite users
Thanks Mark and Jonathon. 

Well, the initial problem with traits has morphed into another. 

I have removed multiple directory paths in the PYTHONPATH
and left just one C:\Users\me\Anaconda3

This is where the python libraries are....including mayavi.

But now running 

"from mayavi import mlab"

I get "ImportError: cannot import name 'mlab' from 'mayavi' (unknown location)"

I then attempted to reinstall mayavi and it tells me that 
everything is satisfactorily installed. 

----------------------------------------------
So now I'm wondering what exactly should or should not be in the 
PYTHONPATH  variable

Darren



From: ets-...@googlegroups.com <ets-...@googlegroups.com> on behalf of Jonathan March <jma...@enthought.com>
Sent: Wednesday, July 21, 2021 9:11 AM
To: Enthought Tool Suite users <ets-...@googlegroups.com>

Chris Barker

unread,
Jul 21, 2021, 6:37:29 PM7/21/21
to Williams, Darren, Jonathan March, Enthought Tool Suite users
No idea if there is anything specific to Mayavi, but as a rule, you do not want to use PYTHONPATH at all.

In the modern world of multiple python versions, virtual environments, conda environments, etc, PYTHONPATH is the road to heck!

I would start from scratch with a brand new Anaconda or miniconda install (I really prefer miniconda) -- then go from there.

You may want to consider using conda environments as well, if you have multiple projects that may have different requirements.

Final word of advice for using conda / Anaconda:

Even if you have only one environment (the base environment), keep a list of all the packages you use and want installed. Then if things get tangled up with conda, you can easily start fresh with a:

conda install --file my_conda_packages.txt

In fact, conda has an easier time resolving dependencies if you hand them all in at once, so when you need a new package (or an updated one), rather than installing or updating just that one, you can add it fo your my_conda_packages.txt file, then do the same command above.

HTH,

-CHB




--

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris....@noaa.gov

Jonathan March

unread,
Jul 21, 2021, 10:12:00 PM7/21/21
to Enthought Tool Suite users
Strongly agree about not using PYTHONPATH, certainly not in this way.
The other point to consider is that you may have directories in your PATH environment variable that are causing library load problems. As a starting point, nothing in your Python universe other than miniconda itself (or equivalently edm) (NOT any of the Python environments that they create) should be on PATH.

Williams, Darren

unread,
Jul 22, 2021, 9:58:33 AM7/22/21
to Jonathan March, Enthought Tool Suite users
Thanks Everyone. 

This whole fiasco started weeks ago with a fresh anaconda
install on a new Windows 10 machine. Naturally I wanted the latest
versions of Python (>3.8), Spyder (5.0), VTK (>9), PyQt5, and Mayavi (>4.7.2)

and I carefully followed all the installation instructions on 

The latest VTK wheels are available on all the major platforms (Windows, MacOS, and Linux), but only for 64 bit machines. Python 3.x is fully supported on all these operating systems and Python 2.7.x on MacOS and Linux.
But what I've found is that the commands 

pip install mayavi
conda install mayavi
etc.

either hang on install or launch errors about compatibility 
issues between my version of Python and the
desired versions of VTK and Mayavi.

Before the "Troubles" (Irish) I was happily running 
Mayavi (<4.7), vtk (8.0 ish), and Python (<3.8),
and will try to recover the past successes today with a 
fresh conda install and downgrades to various packages. 

It seems that these compatibility issues are relatively 
common reading through the web chats. And what works for one 
programmer, doesn't work for another. 

Somewhere on the Mayavi installation website, there should be a 
note about what versions of Mayavi and VTK will work or won't 
work with conda, and directions for the user on how to override 
or correct the conda install to get the appropriate version
of Maya. 

I have abandoned use of PYTHONPATH as advised and will look 
into use of the my_conda_packages.txt file that Chris described. 

Again, I appreciate the help!

Darren

Sent: Wednesday, July 21, 2021 10:12 PM

Chris Barker

unread,
Jul 22, 2021, 1:52:36 PM7/22/21
to Williams, Darren, Jonathan March, Enthought Tool Suite users
On Thu, Jul 22, 2021 at 6:58 AM Williams, Darren <dmw...@psu.edu> wrote:
This whole fiasco started weeks ago with a fresh anaconda
install on a new Windows 10 machine. Naturally I wanted the latest
versions of Python (>3.8), Spyder (5.0), VTK (>9), PyQt5, and Mayavi (>4.7.2)

and I carefully followed all the installation instructions on 

So here's one more piece of advice -- try not to mix pip and conda.

If you are using Anaconda, then use conda to install everything you can. Only if there's no way to install via conda, then try pip -- but frankly, that only works well for pure python packages. The Mayavi / VTK stack is a complex and messy beast -- exactly what conda was designed for.
But what I've found is that the commands 
pip install mayavi
conda install mayavi

if you did pip first, that could cause issues :-(
 
Somewhere on the Mayavi installation website, there should be a 
note about what versions of Mayavi and VTK will work or won't 
work with conda, and directions for the user on how to override 
or correct the conda install to get the appropriate version
of Maya.

The entire point of conda is to address these issues. If you use conda to install the whole stack (which you should), and it ends up with an incompatibility, then that's a bug in the conda packages :-(

(key point: conda was designed to support non-python packages and their dependencies, like VTK. pip was not -- it's going to struggle with these kinds of complex non-python dependencies)

So a bug report to the package creator (anaconda or conda-forge) is the way to go.

NOTE: if you can't get the recent versions you want for anaconda out of the box, you may want to add the conda-forge channel -- it has more of the "latest and greatest". However, the downside of that is that there may be more compatibility issues -- though it's worked very well for me for the last couple years.

-CHB

Reply all
Reply to author
Forward
0 new messages