Integrating mayavi for 3D visualization
The first step was to install the library. I initially tried using a virtual environment (venv) and pip install mayavi
, but this resulted in compilation errors. I then decided to use conda instead, hoping for better dependency management. My attempt involved the following commands:
# install all the libraries in the environment
conda install -y -c conda-forge mayavi=4.8.2
conda remove -y qt6-main
conda install -y pep8 flake8 matplotlib Pygobject scipy
Even with conda, I encountered an issue. The latest Mayavi conda version and its dependencies created conflicts, specifically concerning Qt6. I then decided to use a specific Mayavi version (4.8.2 in my case) and manually remove Qt6 to resolve these conflicts. This approach allowed me to establish a working environment for Mayavi. This specific version also suited my needs and integrated well with my other tools. I tested this configuration on my primary workstation and found it stable for my immediate use.
Mayavi, while powerful, offers a relatively bare-bones set of primitives. I quickly realized that I would need to create some basic building blocks to streamline my workflow. One of the first functions I developed was for plotting arrows, which I frequently need for visualizing vectors and coordinate axes. I created the following plot_arrow
function:
import numpy as np
def plot_arrow(mlab, start, direction, color=(0, 0, 0), thickness=1.0):
"""
Plots an arrow
Args:
mlab: The Mayavi mlab object to use for plotting.
start: The starting point of the arrow as a 3-tuple (x, y, z).
direction: The direction vector of the arrow as a 3-tuple (dx, dy, dz).
color: The color of the arrow as a tuple (default: black).
thickness: The thickness of the arrow shaft and tip (default: 1.0).
"""
x, y, z = start
dx, dy, dz = direction
magnitude = np.sqrt(dx**2 + dy**2 + dz**2)
# Use mlab.quiver3d for arrow plotting
arrow = mlab.quiver3d(x, y, z, dx, dy, dz, color=color, mode='arrow',
scale_factor=1.0)
# Adjust the thickness of the arrow
arrow.glyph.glyph_source.glyph_source.shaft_radius = 0.0045 * thickness \
/ magnitude
arrow.glyph.glyph_source.glyph_source.tip_radius = 0.007 * thickness \
/ magnitude
arrow.glyph.glyph_source.glyph_source.tip_length = 0.1 / magnitude
This function uses mlab.quiver3d
to create the arrow and then adjusts the shaft radius, tip radius, and tip length to control the visual appearance based on the arrow’s magnitude and the desired thickness. This was important to me, as I needed consistent visual representation regardless of arrow length. The first use case I had in mind for this function was drawing coordinate axes within my 3D scenes.
This plot_arrow
function became a valuable building block in my Mayavi toolkit. It allowed me to quickly and easily add arrows to my visualizations, particularly for representing coordinate axes. This simple yet effective function significantly improved my workflow and made working with Mayavi more efficient.
This initial setup and the creation of this basic primitive were crucial first steps in integrating Mayavi into my engineering toolkit. While the initial installation presented some challenges, resolving the dependency conflicts allowed me to establish a stable working environment. The plot_arrow
function addresses a key need for basic geometric representation and sets the stage for building more complex visualizations. In future posts, I plan to share other custom primitives and techniques I developed to further enhance my 3D visualization capabilities with Mayavi. This includes tools for creating other geometric shapes, handling complex meshes, and improving the overall visual presentation of my engineering data.