Understanding these key concepts makes Mechanical's scripting easier to learn:
Mechanical APIs can be used to access much of Mechanical, including tree objects and their properties.
It is important to understand a key but subtle distinction between two APIs related to the mesh:
Model.Mesh
accesses the objectMesh
in the Mechanical tree, containing APIs for that object's properties in the Details view. It can be used to add and access mesh controls.DataModel.MeshByName("Global")
accesses FE information (node and element locations, element connectivity, and so on).
It is important to understand a key but subtle distinction between two APIs related to the geometry:
DataModel.GeoData
accesses the underlying geometry attached to Mechanical.Model.Geometry
accesses the objectGeometry
in the Mechanical tree.
The underlying
Geometry
, much like theTree
, is hierarchical. For example,DataModel.GeoData.Assemblies[0].Parts[0].Bodies[0].Volume
accesses the volume for the first part in the first (and only) assembly.All bodies have parts as their parents, even if they are not multibody parts. This is important to understand in both
GeoData
and when traversing the objectGeometry
of theDataModel
. Although a part might be hidden from the Mechanical interface, the part is always there. For more information, see Multibody Behavior and Associativity in the Ansys Mechanical User's Guide.For more information on accessing the properties of an object, including those for traversing the geometry, mesh, simulation, and results, see Mechanical APIs.
It is often useful to store variables to access later rather than using the same API over and over. For example, using the following three commands is better than duplicating the
DataModel.MeshByName(“Global”)
expression in the second and third commands:mesh = DataModel.MeshByName("Global")
mesh.ElementCount
mesh.NodeCount
Looping is a fundamental concept in any scripting language. Within the object
Geometry
, you can use loops to add, modify, or export property data. The following script loops over all contacts and change their formulation to MPC:contact_region_list = DataModel.GetObjectsByType(DataModelObjectCategory.ContactRegion)
For
contact_region
incontact_region_list
:contact_region.ContactFormulation = ContactFormulation.MPC
The object
Transaction()
can be used to speed up a block of code that modifies multiple objects. This object ensures that the tree is not refreshed and that only the bare minimum graphics and validations occur while the transaction is in scope. The objectTransaction()
should only be used around code blocks that do not require state updates, such as solving or meshing. For example, to avoid redundant work from each addition, the following example use the objectTransaction()
before code that adds many objects:with Transaction(): for bodyId in bodyIds: ...
If you cannot find what you want, check the attribute
InternalObject
. While the ACT API has “wrapped” many useful aspects of Mechanical, it has not wrapped everything, for various reasons. Many API objects have anInternalObject
attribute that you can use to find additional capabilities that are not formally exposed in ACT.
For more information about how to complete many types of Mechanical tasks using scripts, see the examples in these sections: