Mastapy Features
This article is an explanation of some of the basic features of mastapy
that are present in version 11.0.
Full Intellisense Support
The mastapy
package makes great using of type hints throughout the package allowing for comprehensive support of third-party tools such as intellisense, type checkers and linters.
Specifically in Visual Studio Code proper Python intellisense can be used by swapping to the Jedi
Python language server.
Open your settings.json
file and append the following line to turn on the Jedi
Python language server.
"python.languageServer": "Jedi"
Information on where to find settings.json
can be found here.
The following animated GIFs demonstrate the new intellisense in various scenarios.
Intellisense allows you to quickly find methods or classes while importing. It shows you all of the possible options for each sub-package or module and will intelligently search through the options to find the one you want.

All mastapy
objects have proper intellisense support. This means you can find all properties and methods on any mastapy
object with ease.

You can view the types and signatures of objects, methods and properties by hovering over them.

Every method and property has documentation telling you the expected arguments and the full paths of all types. This can be useful if you need to know where to import certain types from (for instance, the AnalysisType
enum in the above example.)
mastafile.py
A feature unique to mastapy
is the mastafile.py
functionality. Any file called mastafile.py
stored in the same directory as your Python scripts will be executed BEFORE any script is executed. This means mastafile.py
is perfect for containing initialisation procedures, such as the mastapy.init
call that has to be made before executing external scripts. This also means you only have to write the mastapy.init
call once in the mastafile.py
file and have it automatically executed for all of your scripts.
It is highly recommended if you are maintaining a lot of scripts to use a mastafile.py
file.
The following is an example mastafile.py
file that initialises mastapy
.
# mastafile.py
from mastapy import init
init('MY_MASTA_DIRECTORY')
MASTA Property Hooks
An advanced feature of mastafile.py
is the ability to write pre/post MASTA property hooks. These are methods that are executed before or after a MASTA property is executed and can be used for repetitive, rudimentary setup or tear-down procedures. Note that the hooks also require one parameter for your MASTA object. However, the parameter does not have to be typed like in the following example.
Hooks can be chained! This means you can add multiple masta_before
or masta_after
decorators to your MASTA properties.
Add methods to be used as hooks to
mastafile.py
.# mastafile.py import mastapy masta_api_dir = r'MY_MASTA_DIRECTORY' mastapy.init(masta_api_dir) def pre_hook(design): print('Before!') def post_hook(design): print('After!')
Add additional
masta_before
andmasta_after
decorators to yourmasta_property
decorated methods.from mastapy import masta_property, masta_before, masta_after from mastapy.system_model import Design @masta_before('pre_hook') @masta_after('post_hook') @masta_property(name='Hooks Test') def my_masta_property(design: Design): print('Hello world!')
By executing the Hooks Test
property from MASTA, you will get the following output in the MASTA scripting console:
Before!
Hello world!
After!
Pythonic Naming Conventions
All sub-packages, methods, properties and parameters use the snake_case
naming convention instead of the PascalCase
naming convention found in the C# API. This change was made to adhere to Python coding standards, as detailed in the PEP 8 Style Guide. This means all of your code using the mastapy
package will look a lot more Pythonic.
Quality of Life Improvements
There have been a lot of small, quality of life improvements. The following lists a small number of these.
Properties with generic types such as
Overridable
,EnumWithSelectedValue
andListWithSelectedItem
can be implicitly set and retrieved.# Before my_load_case.InputPowerLoad.Torque = Overridable[float](100.0) # After my_load_case.input_power_load.torque = 100
All data structures such as lists and dictionaries use proper Python data structures. This means all Python methods for interacting with or manipulating data structures will work as expected.
# Before num_points = my_shaft.ActiveDefinition.OuterProfile.Points.Count # After num_points = len(my_shaft.active_definition.outer_profile.points)
A whole host of linear algebra classes are available that are unique to the Python API. More information on this can be found here.
Many other conversions to Python types for complex numbers, images and more.
Full Legacy Support
To ensure any scripts written with old versions of mastapy
still work, mastapy 11.0
has full legacy support. You will not need to update any scripts to the new API.