How to run a Python script automatically in MSC Apex

There may be a number of things you repeat every time you create a new model, like creating a material or defining a number of standard beam sections. It's tedious work and prone to error, but you don't have to. You can simply have a simple Python script (which you can record as a macro), do it for you every time you start MSC Apex or when you create a new model.

The sample script below creates two materials:

import apex

apex.setScriptUnitSystem(unitSystemName = r'''mm-kg-s-N''')
model_1 = apex.currentModel()

def CreateMaterials():
    
    material_1 = apex.catalog.createMaterial(
    name = "Material 1", 
    description = "", color = [ 128, 128, 128 ]
    )
	
    material_1.update(
    name = "Steel",
    elasticModulus = 200000.,
    poissonRatio = 0.3,density = 0.00000785 
    )
	
    material_2 = apex.catalog.createMaterial(
    name = "Material 2", 
    description = "", color = [ 217, 217, 217 ]
    )

    material_2.update(
    name = "Aluminium",
    elasticModulus = 70000.,
    poissonRatio = 0.33,density = 0.0000027 
    )

CreateMaterials()

The content above is simply saved to a text file with the .py extension and set up in MSC Apex to execute whenever a new default model is created (on start up of MSC Apex) or when the user creates a new model (File > New).

The only content from the script above, that you need to copy and reference in MSC Apex, to set it up for automatic execution, is the function name, e.g. CreateMaterials, and the name and location of the file, as shown below:

Where to define when and what function needs to execute in MSC Apex

For the example above, the function CreateMaterials from the script CreateMaterialsOnStartUp.py will execute every time the default new model is created when starting MSC Apex, or when the user creates a new model.

FYI: The first 4 lines of code:

import apex
from apex.construct import Point3D, Point2D

apex.setScriptUnitSystem(unitSystemName = r'''mm-kg-s-N''')
model_1 = apex.currentModel()

are standard lines of code that any MSC Apex script starts with, regardless of what follows.

The line setting the unit system should be self explanatory but is important to note that should you want to work in a different unit system, this line has to be edited.

To get started creating your own scripts, simply record a macro while executing some basic functions to see what the created Python code looks like, like the code of this macro which was created when recording the task of creating a new material:

How to start recording a macro in MSC Apex

Execute all tasks to record in the macro
Stop the recording
View the content of the recorded macro

The macro recorded above can be run directly, as-is, any time those exact steps are required to be performed.

When you wish to set the above script to be run automatically, a few minor edits are required as explained below:

Compare the content of a recorded macro and a script that can be set to automatically execute in MSC Apex

The example above shows the 4 standard (required) lines of code in blue in both the recorded macro and the edited script, with some additional (optional) settings recorded that can be kept or left out from the edited script.

The only required edit for scrips, which will be set to run automatically, is the definition of a function that isolates the lines of code that MSC Apex has to run under the set condition(s).

This implies that multiple functions can be defined in the same script (*.py file) as well to keep an organised set of functions and each function can also accept its own input arguments as required, opening up the full power of customized scripting.

A strong community of MSC Apex script writers already exist with many custom and specialized scripts already available. Feel free to contact us to be connected or to inquire if a certain function you require might already exist in an available script.

Knowledge is power!

If you like knowing what is new in simulation technology, you're at the right place. Register for our monthly newsletter here:

Don't ever risk your career by using cracked software!

THE EXCUSES:
I only used it for my studies, I only wanted to evaluate the software, I didn't use if for commercial purposes, I only installed it, but never used it... Unfortunately, no reason holds up in court for the possession of stolen goods.

THE FINE?
The full commercial purchase price of the software (for everything you had access to, whether or not it was even used!) 

Students (under- or post-graduate) also has no recourse since they can get access to free legal student versions, or the full commercial versions through their respective universities.
If you wish to evaluate any software or module that you do not have access to, please contact us, we will gladly arrange for evaluation licenses, even emergency licenses. 

There is just no reason to access illegal software.
crossmenulist linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram