Generating hexahedral meshes with Python scripting
Learn how to generate high-quality hexahedral meshes using Python scripting. Explore mesh generation using methods like revolve, extrude, and sweep, and learn how to analyse mesh quality.

In simulations like Finite Element Analysis (FEA), having a good-quality mesh is important—not only for accurate results, but also for faster and more efficient computations. One of the best options for this is using hexahedral elements (or hex elements), which are known for their accuracy and performance. In this article, we will provide some background information on hexahedral meshes and look at an alternative workflow for creating high-quality hex meshes directly with the Python scripting offered through HelloTriangle.
Hexahedral vs. tetrahedral elements
The two most common element types used when simulating 3D solids are hexahedral and tetrahedral elements. Each comes with its own strengths and trade-offs.
Hexahedral elements
Pros:
Typically very accurate (linear as well as quadratic elements)
Computationally efficient, often requiring fewer elements for the same level of accuracy
Cons:
Difficult to apply to complex geometries
Labor-intensive meshing
Tetrahedral elements
Pros:
Easy to generate, even for complex geometries
Quadratic tetrahedral elements typically yield good results
Cons:
Lower accuracy with linear elements
Greater element count usually required for convergence
While tet elements excel in meshing arbitrary shapes, hex elements remain the gold standard for structured domains due to their numerical performance.
A new approach: geometry and mesh together
Traditionally, mesh generation begins with the creation of a 3D CAD model, which is then discretized into a mesh. HelloTriangle proposes a more integrated workflow: using Python scripting to generate both the geometry and the mesh at the same time. This method is explored in detail in this article on parametric mesh modeling, and offers two major benefits:
It avoids common issues encountered when trying to mesh complex CAD geometry and helps making hex mesh generation more efficient - solving a key drawback of traditional methods.
It enables automating mesh sensitivity studies by adjusting mesh density through script parameters
This scripting-based workflow is particularly useful for research and engineering teams looking to automate and scale simulation pipelines.
Methods for hexahedral mesh generation
HelloTriangle supports several techniques for hex mesh generation. Many of these begin with the creation of a quadrilateral (quad) mesh, which is then used to create a 3D hexahedral mesh.
1. Revolve
In this method, a 2D quad mesh is rotated about an axis to create a structured 3D hex mesh. This is illustrated in the example below, where this technique is used to create a pipe with a 90 degree bend. The Python code is provided in the next section.

2. Extrude
Extrusion involves extending a 2D mesh along a straight path. We will use this technique to create a straight tube, that we can combine with the previously generated 90 degree bend (code is provided in the next section).

If you are interested to learn more about this method, please check the dedicated example on mesh extrusion.
3. Connect
Another useful method is to connect 2 meshes that are topological identical. This means that the two meshes need to have the same overall structure and connectivity, and only the positions of the vertices are different.
If you want to learn more about this method, please check the example on mesh merging. In this example, a quad mesh is generated by connecting two meshes consisting of line elements, but this technique can of course be used to generate hexahedral meshes.
4. Sweep along a path
This method sweeps a cross-sectional mesh along a custom path, ideal for creating curved geometries. A quad mesh can be swept and converted into a hexahedral volume mesh using this approach.
If you want to learn more about this method, please check the example on sweeping a mesh along a path. In this example, a quad mesh is generated by sweeping a mesh consisting of line elements, but this technique can of course be used to generate hexahedral meshes.
The above-described approaches allow users to generate high-quality hexahedral meshes programmatically, providing control over every detail of the mesh structure.
Example: 90-degree pipe with flanges
To illustrate the power of Python-based hex mesh generation, we will now combine some of the previously described methods to generate a 90-degree pipe bend with flanges. This example nicely demonstrates:
How to create both geometry and mesh in a single script
How to adjust mesh density through parameter changes
from hellotriangle import shapes
# mesh parameters
nt = 2 # number of elements along the thickness
nc = 36 # number of elements along the circumference
n90 = 10 # number of elements along the 90 degree bend
ns = 5 # number of elements along the straight parts
ntf = 3 # number of elements along the thickness (flanges)
nrf = 5 # number of elements in the flanges (radially)
# geometry parameters
ri = 1.0 # inner radius
th = 0.2 # pipe thickness
thf = 0.3 # flange thickness
rf = 2.2 # flange outer radius
ls = 2.0 # length of straight pipe segments
r90 = 2.0 # radius of 90 degree segment
# generate quad pattern
cirInner = shapes.circle(nc).scale(ri)
cirOuter = shapes.circle(nc).scale(ri+th)
quad = cirInner.connect(cirOuter, div = nt)
# generate straight part through extrusion
straight = quad.extrude(ns, dir = [0.0, 0.0, 1.0], length = ls)
# generate 90 degree bend by revolving
quadTrl = quad.translate([r90, 0.0, 0.0])
bend = quadTrl.revolve(n90, axis = 1, angle = -90.0)
# generate flanges
cirFlange = shapes.circle(nc).scale(rf)
quadFlange = cirOuter.connect(cirFlange, div = nrf)
flange = (quad+quadFlange).extrude(ntf, dir = [0.0, 0.0, 1.0], length = thf)
# assemble all components
flangeTrl = flange.translate([0.0, 0.0, -thf])
part1 = (flangeTrl + straight).translate([r90, 0.0, -ls])
part2 = part1.translate([-2*r90, 0.0, 0.0]).rotate_Y(90.0)
pipe = part1+bend+part2
# draw
draw(pipe, color = "grey")
Let’s now change some of the mesh related parameters to obtain a different mesh resolution (see figure below), which is of course very important for running mesh convergence studies.

Mesh quality analysis
Generating a mesh is just the beginning. The quality of the mesh plays a significant role when running simulations, and may affect simulation accuracy, performance and stability.
Many metrics can be used for assessing mesh quality, and here we will focus on the scaled Jacobian. This metric evaluates how much an element deviates from the ideal shape, based on the transformation between the element’s parametric and physical space. A value of 1.0 indicates a perfectly shaped element, while values approaching 0 suggest increasing distortion.
Scaled Jacobian values range from 0 to 1 for valid elements
Values close to 1 indicate better quality
Lower values may cause convergence issues or inaccuracies in simulation results
HelloTriangle makes it easy to compute the scaled Jacobian for all elements in your mesh. This allows users to identify low-quality regions, adjust the mesh generation, and ensure robust simulation performance.
The code below shows how to obtain the scaled Jacobian, and how to get this parameter for the worst element in the mesh.
#mesh quality assessment
scaledJac = pipe.scaledJacobian()
print(scaledJac.min())
Exporting hexahedral meshes
The resulting hex meshes can be easily exported to some of the most commonly used FEA packages like Abaqus and Ansys.
# export Abaqus input file
pipe.write('pipe.inp")
# export Ansys msh file
pipe.write('pipe.msh')
Conclusion
Creating hexahedral meshes via Python scripting unlocks new levels of automation, reproducibility, and precision in simulation workflows. Scripting gives you full control over geometry, mesh density, and quality.
With HelloTriangle, you can go from concept to simulation-ready mesh entirely in Python, and avoid the typical time-consuming meshing process.
👉 Ready to generate your first hex meshes? Sign up for HelloTriangle today.