Everyone loves freedom. So do CAE engineers when it comes to using the Python API in ANSA.
The Python API that ships with ANSA is great for day-to-day scripting involving easy and quick automation. It’s when one tries to expand his/her codebase to perform tasks that require external Python modules not included with ANSA. Though ANSA plays conservative when it comes to using its libraries like any other proprietary software, it does provide a way to add external libraries that could work in sync with its inbuilt Python. The following section describes how one may import external modules inside ANSA and a slick way to manage everything from one place.
- ANSA – Preprocessor
- Anaconda Python (Miniconda)
- Python 3.3 environment
Miniconda is the basic stripped down version of Anaconda and this would be sufficient for our purposes.
Download and install Miniconda (Python 3.6) from https://conda.io/miniconda.html. Make sure to remember the installation path and add it to the system environment path.
Note: From now onwards, we will be using the system Terminal to work with Miniconda. If in case the commands were not found, please make sure the following paths are added to the system paths.
Creating an environment for Python 3.3
As of 2018, both ANSA and META are shipped with Python 3.3. That would mean all external python packages to be installed and imported from within ANSA/META work best if done in the same environment.
A new environment with the name “python33” can be created using the command
$ conda create --name python33 python=3.3
Activating the new environment
Activate the new environment using the command
$ source activate python33
In the case of Windows, use the following command in the command line window
$ activate python33
Once activated, you must notice the environment name in the terminal prompt. In this case
Installing new packages
Packages like scipy which are available in the default Anaconda repo can be installed by
(python33) $ conda install scipy
Often times, the packages one is looking for are not available or do not have the latest versions of the module.
condo-forge is a GitHub organization containing repositories of the latest conda recipes. One can make this repo the default search place for Miniconda by executing the following command.
(python33) $ conda config --add channels conda-forge
One could also use pip to install the modules. It’s best practice not to use both conda and pip in the same environments.
Importing the installed modules in ANSA/META
Before trying to import any of the modules in ANSA, one needs to add the paths of the installed modules. This can either be implemented in the ANSA_TRANSL.py or it can be added in the first lines of any code in the default Script Editor.
The best practice is to add the following lines in the ANSA_TRANSL.py and not worry about it in the scripts.
import sys INSTALLATION_DIR = "/path/to/the/installation/dir/" PYTHON33_ENV_DIR = INSTALLATION_DIR + "/envs/python33/" sys.path.append(PYTHON33_ENV_DIR + "/lib/python3.3/site-packages") # In the case of Windows, please use the following path # sys.path.append(PYTHON33_ENV_DIR + "/Lib/site-packages") # Import the installed external modules here import scipy
Note: The user will notice an import error while opening ANSA if the imported module is not present in the given path.