Python API Recording#
The recording tool lets you quickly transform the interactive actions performed in the HyperMesh client into a Python code. The recorded functions can be reviewed, parametrized and the generated code can be printed in the Python console or saved as a file.
From the Developer ribbon, click on Recording to activate the Recording toolbar. If the Developer ribbon is not active, you can activate it via View > Ribbons.
Figure 1. Developer ribbon with Recording.
The toolbar will appear at the bottom of the graphics area, next to the Visualization toolbar.
Figure 2. Recording toolbar.
Python recording works on-demand, i.e. you can start and stop the recording to capture only the desired sequence of actions. The recorded code will be generated in a form of a run function. After the recording is stopped, you can parametrize the code beforehand – you can replace the argument values in each function with Python variables, specify the default values if desired, and expose these parameters as arguments of the run()
function in the generated code.
import hm
import hm.entities as ent
def run(file_name : str, comp_name : str = "new_component"):
model = hm.Model()
# [TIP]: Command triggers popup window. Enable 'Ignore popups' to ignore it when running the script.
model.deletemodel()
# [TIP]: Command triggers popup window. Enable 'Ignore popups' to ignore it when running the script.
model.readfile(filename=file_name, load_cad_geometry_as_graphics=0)
component = ent.Component(model, name=comp_name)
User Interface#
The toolbar provides the main control point to start and stop the recording.
Figure 3. Recording toolbar ready to start the recording.
Figure 4. Recording toolbar during the recording.
Once you stop the recording, the below dialog will be displayed and the toolbar will be automatically hidden.
Figure 5. Recording dialog showing the recorded functions.
The Recorded Functions tab shows the API functions corresponding to the recorded actions listed in the order of execution. For each function, the tree lists all its arguments, their types and their values. The Parameter column allows user to assign a parameter to an argument. The values listed in the combobox are filtered by parameter/argument type.
New parameters can be created by right-clicking and selecting Create Parameter.
Figure 6. Right-click context menu in Recorded Functions tab.
Then a pop-up dialog box will appear, letting you define the Name, Type, and optionally the Default Value of the parameter. The default names are automatically incremented and no duplicates are allowed. The Default Value field uses a placeholder to provide a hint to the user how to format the input value.
Figure 7. Create Parameter dialog.
All the parameters are listed in the Parameters tab. From here, you can edit the parameters (change the Name, Type, or Default Value), or create/delete a parameter via right-click context menu.
Figure 8. Recording dialog with a list of all defined parameters.
Output Settings#
The user can control several aspects of the generated code. The options can be accessed via the hamburger icon ≡ in the top right corner of the Recording dialog.
Figure 9. Output settings.
Option |
Description |
---|---|
Code destination |
Defines where the generated code is output. |
Generate verbose code |
Expands the code by creating individual variables for every argument. |
Show equivalent Tcl commands |
Includes equivalent Tcl commands as comments. |
Include all functions |
Includes view, undo/redo, and similar functions. |
Enable deprecated functions |
Creates a debug model object that provides access to the deprecated functions. |
Append collections |
Appends collections when creating collections by attached/adjacent. |
Ignore popups |
Inserts model.hm_answernext(“yes”) before functions that can trigger a popup. |
Use ranges for ID lists |
Compresses full ID lists using range function. |
Include all split entity classes |
Generalizes the code to act on all split entity classes. |
Include tips in the comments |
Includes tips about available options in the comments. |
Generate Python Code#
From the Developer ribbon, click on Recording tool. This will bring up the Recording toolbar at the bottom of the graphics area.
Note
If the Developer ribbon is not visible, you can enable it via View > Ribbons > Developer.
Start the recording by clicking on the toolbar button.
Perform the interactive operations in the HyperMesh client that you want to record.
Stop the recording by clicking on the toolbar button.
Upon stopping the recording, the Python API Recording dialog will pop up allowing you to add parameters and change the settings for the code generation.
Click Generate to output the Python code.
Limitations#
Python recording also captures the logic used in the Advanced Selection to select the entities. For example, selecting elements by property by ID will produce the following code:
properties_collection = hm.Collection(model, hm.FilterByEnumeration(ent.Property, ids=[6]))
collection = hm.Collection(model, hm.FilterByCollection(ent.Element, ent.Property), properties_collection)
This mechanism is currently limited to guidebars with a single selector. Guidebars with multiple selectors and selectors inside the Entity Editor will be supported in the future.
Selection options reverse, by attached, by adjacent, and by face are not yet captured by Python API recording.