Chapter 21: Python Code

The Python Code object enables you to execute python code in response to events occurring in Mechanical for a given workflow. When inserted, an editor pane displays automatically and includes features such as auto-completion and syntax highlighting. The events related to Python code execution can be split into two main categories:

  1. Use python API to query information about the data model and inject MAPDL commands into the solver input file at different points in the construction of the input file.

  2. Execute python code based on events in Mechanical.


Recommendation:  For security purposes, the application does not automatically connect Python Code (object) callbacks when you launch Mechanical. You must first activate the Connect/Run Python Code Objects when Mechanical is Launched preference contained in the Mechanical category of the Workbench Options dialog (Tools > Options > Mechanical).

Python code callbacks can execute code on a host system with the same privileges as Mechanical. Therefore, when you open a project that includes a Python Code object, and that object contains unreliable code, your system could become exposed to external threats (system scans, data deletion, installation of undesired software, etc.).

You can activate the above preference to automatically connect Python Code objects when opening projects, however, Ansys recommends that you keep this option unchecked if you receive and run projects from users other than yourself.



Important:  Also note that if the Python Code object includes scripts related to Design Points, you must activate the above option.