ANSYS: Easy Tutorial on Python Automation

Johnny Chen
3 min readDec 22, 2021

This semester, I took Finite Element course and there are a lot of cumbersome work to set on ANSYS (inputting material, setting mesh, boundary conditions, etc.) Therefore, I was thinking about can I use some method to expedite the process and automation the repeating curser-click process.

And I found out that there is one scripting language called ANSYS Parametric Design Language (APDL) that is used for engineers to set up and perform FEA before friendly interface came along. While it is harder to learn, the scripting allows its users on automation. In the newer version of ANSYS, users can use Scripting or Python Code to control ANSYS to get rid of repeating tasks. In this article, a simple cantilever beam analysis subjecting to a point load at its tip is performed.

Figure 1: Setting up 3D beam in SpaceClaim

Then, the first thing we need to know is the hierarchy of ANSYS Mechanical. ExtAPI.DataModel is used to access the Mechanical data model [1], and by adding the ending parenthesis “( )” it indicates that it is a method.

Figure 2: ANSYS scripting hierarchy [1]
Figure 3: The red button means record which can be use to know the movement that we do in script

First, we used some nomenclature to denote the models under Mechanical in Figure 4

Figure 4

Then, we need to generate mesh. By using command “mesh.GenerateMesh()” we are able to generate mesh. But what if we want to set different mesh methods to different components? In Figure 5, we use automatic mesh method to mesh the 3D beam.

Figure 5: Using automatic method to generate mesh

Next, both essential boundary conditions (displacement) and natural boundary conditions (applied force/tractions) need to be set under Static Structural under Model. In Figure 6, we added fixed support on the end of the beam. Noted that we can get each element ID in Selection Information and input element ID into location. Since multiple components may be set in one Name Selection, we should put [0] to denote which is the component.

Figure 6: Applied both fixed support and line pressure

A line pressure is applied on the edge of another end. Here I set the edge that we are going to applied line pressure Name Selection as “front”. How to define the force direction as well as the place it applied?

If we change Define By of force from vector to component, we need this command “LoadDefineBy.Components”. Next, we change the magnitude of the force by using Out.SetDiscreteValue( int index, Quantity).

In the end, we need to solve the displacement and post process the stress. By using command “analysis.Solution.AddTotalDeformation()“ and “analysis.Solution.AddEquivalentStress()” and “model.Solve()” we can get our final result.

Figure 7: All code listed

Useful tutorial:

Ansys Mechanical Scripting Training (

ANSYS ACT 2019 R2 Release API and XML Online Reference Guide for Mechanical (


  1. Automation in Ansys Mechanical using Python,