Mechanical scripting API stubs for PyMechanical.
Project description
Overview
PyMechanical Stubs generates __init__.py files from assembly files in the Mechanical application to create python files that can be used for autocomplete with PyMechanical. Stubs are generated for each version of Mechanical, starting with 2024 R1.
clr-stubs generate Python stubs for .NET assemblies using pythonnet. These stubs are intended to be used by the autocomplete engine of editors like Atom, Sublime, and VS Code, as well as for python documentation generation (for example, with Sphinx)
Why clr-stubs?
If you are writing python code using .NET modules via pythonnet’s clr.AddReference, your IDE’s autocomplete (which usually runs python) will not be able to follow any .NET namespaces or libraries.
The workaround in clr-stubs is to create ‘stubs’ or ‘fakes’ with the same namespaces, types, and metadata that would typically be available in a pure python library. The ‘stubs’ can then be used by your IDE’s autocomplete.
Manually create __init__.py files
Install Mechanical 2024 R1 or R2.
Note
Ensure the environment variable, AWP_ROOTDV_DEV, is set to the location of Mechanical 2024 R1 or R2 (C:\Program Files\Ansys Inc\v242).
Clone the repository.
git clone https://github.com/ansys/pymechanical-stubs.gitRun stub_generator/create_files.py to generate the stubs from Mechanical 2024 R1 or R2.
python stub_generator/create_files.pyNote
There may be an Unhandled Exception when the stubs are done running. If the message, “Done creating all Mechanical stubs” appears, proceed to the next step.
Next, create and activate a virtual environment:
python -m venv .venvWindows:
.venv\Scripts\activate.batLinux:
source .venv/bin/activateInstall ansys-mechanical-stubs
pip install -e .Make the Sphinx documentation
make -C doc htmlNote
You can ignore any current warning messages. It is a lengthy process to generate the documentation.
Installation
You can use pip to install PyMechanical Stubs.
pip install ansys-mechanical-stubs
To install the latest development version, run these commands:
git clone https://github.com/ansys/pymechanical-stubs
cd pymechanical-stubs
pip install -e .
Install in offline mode
If you lack an internet connection or you do not have access to the private Ansys PyPI packages repository, you should install PyMechanical Stubs by downloading the wheelhouse archive for your corresponding machine architecture from the repository’s Releases page.
Each wheelhouse archive contains all of the Python wheels necessary to install PyMechanical Stubs from scratch on Windows, Linux, and MacOS from Python 3.10 to 3.12. In addition, you can install the wheelhouse on a new virtual environment that does not include any previously installed dependencies.
For example, on Linux with Python 3.10, unzip the wheelhouse archive and install it with these commands:
unzip ansys-mechanical-stubs-v0.1.9-wheelhouse-ubuntu-latest-3.10 -d wheelhouse
pip install ansys-mechanical-stubs -f wheelhouse --no-index --upgrade --ignore-installed
If you are on Windows with Python 3.10, unzip the wheelhouse archive to a wheelhouse directory and then install using the same pip install command as in the preceding example.
Note
If desired, you can install the wheelhouse on an isolated or virtual system. See Creation of virtual environments in the Python documentation for the required steps.
Basic usage
This code shows how to import PyMechanical Stubs and its basic capabilities:
from typing import TYPE_CHECKING
import ansys.mechanical.core as mech
if TYPE_CHECKING:
import ansys.mechanical.stubs.v241.Ansys as Ansys
geometry_import_group = app.Model.GeometryImportGroup
geometry_import = geometry_import_group.AddGeometryImport()
# Lines that start with "Ansys." will autocomplete as you type
geometry_import_format = (
Ansys.Mechanical.DataModel.Enums.GeometryImportPreference.Format.Automatic
)
geometry_import_preferences = Ansys.ACT.Mechanical.Utilities.GeometryImportPreferences()
Alternatively, you can enable autocomplete in VS Code’s settings.json file with the following steps:
Run pip install ansys-mechanical-core.
Run ansys-mechanical-ideconfig in the terminal to retrieve the information for VS Code’s settings.json file:
ansys-mechanical-ideconfig --ide vscode --target user --revision 242The command returns the path to the settings.json file. Open the file and add the lines from the command output.
Once the settings.json file is updated, you only need to import Ansys to enable autocomplete:
from typing import TYPE_CHECKING import ansys.mechanical.core as mech if TYPE_CHECKING: import Ansys from Ansys.Mechanical.DataModel.Enums import * geometry_import_group = app.Model.GeometryImportGroup geometry_import = geometry_import_group.AddGeometryImport() # Lines that start with "Ansys." will autocomplete as you type geometry_import_format = ( Ansys.Mechanical.DataModel.Enums.GeometryImportPreference.Format.Automatic ) geometry_import_preferences = Ansys.ACT.Mechanical.Utilities.GeometryImportPreferences()
Documentation and issues
Documentation for the latest stable release of PyMechanical Stubs is hosted at PyMechanical Stubs documentation.
In the upper right corner of the documentation’s title bar, there is an option for switching from viewing the documentation for the latest stable release to viewing the documentation for the development version or previously released versions.
On the PyMechanical Stubs Issues page, you can create issues to report bugs and request new features. On the PyMechanical Stubs Discussions page or the Discussions page on the Ansys Developer portal, you can post questions, share ideas, and get community feedback.
To reach the project support team, email pyansys.core@ansys.com.
Credits
This project is inspired by [ironpython-stubs](https://github.com/gtalarico/ironpython-stubs) but is developed from scratch.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ansys_mechanical_stubs-0.1.10.tar.gz.
File metadata
- Download URL: ansys_mechanical_stubs-0.1.10.tar.gz
- Upload date:
- Size: 1.2 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e16f4644a9c63a6700c2c8c612eb2c468f2bb2ac4159f1cc8060ac2dc8522486
|
|
| MD5 |
8dda1a9facce70544d1bcc4f0ba0783a
|
|
| BLAKE2b-256 |
095d00e661a5b502898c6dacbc0a8383454c0ba404ad347fb558a2a49940203f
|
Provenance
The following attestation bundles were made for ansys_mechanical_stubs-0.1.10.tar.gz:
Publisher:
ci_cd.yml on ansys/pymechanical-stubs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ansys_mechanical_stubs-0.1.10.tar.gz -
Subject digest:
e16f4644a9c63a6700c2c8c612eb2c468f2bb2ac4159f1cc8060ac2dc8522486 - Sigstore transparency entry: 1135765820
- Sigstore integration time:
-
Permalink:
ansys/pymechanical-stubs@93354234eedf75275b42bd912febd91ee64ffc57 -
Branch / Tag:
refs/tags/v0.1.10 - Owner: https://github.com/ansys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci_cd.yml@93354234eedf75275b42bd912febd91ee64ffc57 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ansys_mechanical_stubs-0.1.10-py3-none-any.whl.
File metadata
- Download URL: ansys_mechanical_stubs-0.1.10-py3-none-any.whl
- Upload date:
- Size: 1.6 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf35b22d2a19e5964ae9c0f424fad42fe1b9406a2fd6d8e4eeaa52ea38ff5536
|
|
| MD5 |
911bf827f6b42c576c0bf0a42a5dbb42
|
|
| BLAKE2b-256 |
2d7eecb18fa9c98cd39643dbcfedfce95391520fe480c0ea7cc76f4cb79d77d1
|
Provenance
The following attestation bundles were made for ansys_mechanical_stubs-0.1.10-py3-none-any.whl:
Publisher:
ci_cd.yml on ansys/pymechanical-stubs
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ansys_mechanical_stubs-0.1.10-py3-none-any.whl -
Subject digest:
bf35b22d2a19e5964ae9c0f424fad42fe1b9406a2fd6d8e4eeaa52ea38ff5536 - Sigstore transparency entry: 1135765885
- Sigstore integration time:
-
Permalink:
ansys/pymechanical-stubs@93354234eedf75275b42bd912febd91ee64ffc57 -
Branch / Tag:
refs/tags/v0.1.10 - Owner: https://github.com/ansys
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci_cd.yml@93354234eedf75275b42bd912febd91ee64ffc57 -
Trigger Event:
push
-
Statement type: