Set Meshing Options with Python?


I am new to Netget/NGSolve.

I hope you guys can help me, here the manual way I do:

I load a new Geometry into NGSolve.
After that I navigate into “Meshing options…” select “Mash granularity=very coarse” after that under “Mesh Size/Elements per curv…radius” I switch it to the value 5.

After that “Export Mesh” to a *.STL file.

that works fine, but I need to do this automaticly.

I allready wrote a little phyton file:

from netgen.NgOCC import *
from ngsolve import *

geo = LoadOCCGeometry(‘Part.stp’)
mesh = geo.GenerateMesh()

mesh.Export(‘Part2.stl’,‘STL Format’)

But I do not have any idea how I can set the “meshing options” first?

Anyone can help?

thanks much,
regards, mario

no one any idea?

Hi Mario,

There are arguments you can pass to the GenerateMesh() function to control the meshing algorithms. For your case, try the following code

from netgen.meshing import MeshingParameters, meshsize
mesh = geo.GenerateMesh(meshsize.very_coarse, curvaturesafety=5)


thx much for your answer, but it does not generate a mesh:

from netgen.NgOCC import *
geo = LoadOCCGeometry(‘Part.stp’)

from netgen.meshing import MeshingParameters, meshsize
mesh = geo.GenerateMesh(meshsize.very_coarse, curvaturesafety=5)

it just shows me the Part.stp in the GUI, but not the generated mesh.

what wrong with this?

regards, mario

This comes from the console:

Developed by Joachim Schoeberl at
2010-xxxx Vienna University of Technology
2006-2010 RWTH Aachen University
1996-2006 Johannes Kepler University Linz
Including OpenCascade geometry kernel
optfile ./ng.opt does not exist - using default values
togl-version : 2
OCC module loaded
loading ngsolve library
Using Lapack
Including sparse direct solver Pardiso
Including sparse direct solver UMFPACK
Running parallel using 6 thread(s)
importing NGSolve-6.2.1905
(should) load python file ‘’
load OCC geometry Number of colours in STEP File: 1
Colour [1] = STEELBLUE3 : (0.380392,0.576471,0.752941)
Highest entry in topology hierarchy:
1 solid(s)
Traceback (most recent call last):
File “”, line 4, in
ImportError: cannot import name ‘meshsize’ from ‘netgen.meshing’ (C:\Program Files\ngsolve-v6.2.1905\lib\site-packages\netgen\
Finished executing
Preparing visualization (deflection = 0.01) … done

Seems your version of NGSolve is outdated (19.05), I suggest you download a recent version. Note that you might also need to update Python to 3.7.


Thx, update to recent version worked!

One more question, i wanna set other Meshing Options like:

“max mesh size”
“min mesh size”
“mesh size grading”

is there a help file or something, where i can find the correct commands for python?

at the moment i know:

mesh = geo.GenerateMesh(meshsize.very_fine, curvaturesafety=5)

but how i can set “min mesh size” for examble?
how you guys find out the correct paramaters?

thx much!

regards, mario

Hi Mario,

As far as I know, Netgen has no minh setting. Most options are explained by using the python help() function:

from netgen.meshing import MeshingParamters

For a comprehensive list of all options you need too look at the source code:

The different meshsize variants are defined here:



One more question, I am not able to solve:

Under “Meshing Options/General” I can select

  • First Step
  • Last Step
  • Print Messages

and all points under “Additional meshing options”

I allready search for it, is there a way to set those options in python too?

You helped me allready very much but hopefully you can help me again.

thank much!

regards, mario

anyone an idea?

i am not able to find a solution :frowning:

i tried to study the source code, but did not found a solution.

help(MeshingParameters) does not show me a solution, so this means I am not able to set these meshing options with a python script?

Is there any other solution to use the meshing options without interacting with the GUI:

  • First Step
  • Last Step
  • Print Messages

it does not matter if python or not, maybe start netgen.exe with a specific command line?

I am coding a tool for ngsolve, wich is meshing much files at the same time automaticaly every file with a differnt meshing option and the users told me they need to set the meshing options listet above.

if there is no solution they have to interact with the GUI, but this is not a good way i need to do this automaticaly in the background.

first step = perfstepsstart
last step = perfstepsend
in python
and you can set the message level with
ngsolve.ngsglobals.msg_level = 0


thx very much!

i will try as soon as possible…


I just tried the following python script:

from netgen.occ import *
geo = OCCGeometry(“//STI01/STIWA/mail/ParM/ng/projects/parm/abc/Schuettblech.stp”)
mesh = geo.GenerateMesh(meshsize.very_coarse, perfstepsstart=MESHCONST_ANALYSE, perfstepsend=MESHCONST_OPTSURFACE, curvaturesafety=5)
mesh.Export(“//…stl”, ‘STL Format’)

does not work :frowning: → ‘MESHCONST_ANALYSE’ is not defined

what I did wrong?

(should) load python file ‘\STI01\STIWA\mail\ParM\ng\projects\parm\abc\’
Traceback (most recent call last):
File “”, line 2, in
FileNotFoundError: [WinError 3] Das System kann den angegebenen Pfad nicht finden: ‘C:\STI01\STIWA\mail\ParM\ng\projects’
Number of colours in STEP File: 1
Colour [1] = LIGHTBLUE3 : (0.654902,0.760784,0.788235)
Highest entry in topology hierarchy:
1 solid(s)
Traceback (most recent call last):
File “”, line 3, in
NameError: name ‘MESHCONST_ANALYSE’ is not defined
Finished executing \…py
Preparing visualization (deflection = 0.01) … done


having a look at the code, I found a python enum “MeshingStep”:


So just use

from netgen.meshing import MeshingStep

mesh = geo.GenerateMesh(meshsize.very_coarse, perfstepsstart=MeshingStep.ANALYSE, perfstepsend=MeshingStep.MESHSURFACE, curvaturesafety=5)

to generate the mesh.


thanks very much!


regards, mario

The enum “MeshingStep” contains all these options (ANALYSE, MESHEDGES, MESHSURFACE, MESHVOLUME). I copied the export to python in my last post.

Omg i read it to fast, thx man!