Code Parametrization#
Once the recording is stopped, the below dialog is displayed and the toolbar is automatically hidden.
Figure 1. Recording dialog showing the recorded functions.
The dialog provides the following capabilities:
Reviewing the recording functions, their arguments and corresponding values.
Creating variables and assigning them to function arguments
Linking HyperMesh parameter entities to function arguments
Replacing explicitely defined collections/entity lists/entity objects with interactive selection
Controlling the Output Settings
Creating and Assigning Variables#
The recorded argument values can be replaced with Python variables that will be exposed as arguments of the main()
function in the generated code. Optionally, user can define the default value for the variables as well.
The variables can be created in two ways:
Option 1 - Using Create Variables dialog on the Recorded Functions tab
Right-click in the empty space and click Create Variables.
Figure 2. 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 variable. 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 3. Create Variable dialog.
Option 2 - Using the Script Variables tab
Right-click in the empty space and click Add.
Figure 4. Right-click context menu in Script Variables tab with the list of all defined variables.
The Script Variables tab shows all the created variables and allows user to edit/delete the existing ones.
Once the variable is created, it can be assigned to an argument via the comboboxes in the Variables/Parameters column. The variables shown in the comboboxes are filtered based on the argument type. The code snippets below show the generated code before and after assigning the variables.
import hm
import hm.entities as ent
model = hm.Model()
def main():
# [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="C:/WORK/DevBuilds/2025.0.0.24/hwdesktop/demos/hm/bumper.hm", load_cad_geometry_as_graphics=0)
components_collection = hm.Collection(model, hm.FilterByEnumeration(ent.Component, ids=[2, 5]))
collection = hm.Collection(model, hm.FilterByCollection(ent.Element, ent.Component), components_collection)
model.split_elements_by_structure_pattern_freeselection(collection=collection, refine_size=8, adjacent_layers=1)
model.createsolverkeyword(keyword="PSHELL", type=ent.Property)
property = ent.Property(model, 1)
property.includeid = 0
property.PSHELL_T = 3.6
if __name__ == "__main__":
main()
import hm
import hm.entities as ent
model = hm.Model()
def main(file_path : str, elem_size : float, thickness : float = 3.6):
# [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_path, load_cad_geometry_as_graphics=0)
components_collection = hm.Collection(model, hm.FilterByEnumeration(ent.Component, ids=[2, 5]))
collection = hm.Collection(model, hm.FilterByCollection(ent.Element, ent.Component), components_collection)
model.split_elements_by_structure_pattern_freeselection(collection=collection, refine_size=elem_size, adjacent_layers=1)
model.createsolverkeyword(keyword="PSHELL", type=ent.Property)
property = ent.Property(model, 1)
property.includeid = 0
property.PSHELL_T = thickness
Note
The snippet above shows that if any variables do not have a default value, the Python idiom if __name__ == "__main__"
is excluded preventing the code does not get automatically executed when copy-pasted into the console.
Substituting Inputs with GUI Selection#
The Python code is typically recorded with the intention to reuse the code as a generic script/macro. By default, the recorded code captures the exact entity selections that are used during the recording. The explicit selection can be substituted with functions providing interactive selection by checking the corresponding box in GUI Selection column.
The GUI Selection option is available for the following argument types:
Argument Type |
Substituting Function |
---|---|
hm.Entity |
hm.EntityByInteractiveSelection() |
hm.EntityList |
hm.EntityListByInteractiveSelection() |
hm.Collection |
hm.CollectionByInteractiveSelection() |
The entity type is automatically established from the recorded argument value and passed to the interactive selection function.
import hm
import hm.entities as ent
model = hm.Model()
def main():
model.currentcollector(entity_type=ent.Component, name="center")
model.surfacemode(mode=4)
list1 = [ent.Node(model, id) for id in [48, 47, 46, 507]]
list2 = [ent.Node(model, id) for id in [12, 21, 22, 31]]
model.linearsurfacebetweennodes(list1=list1, list2=list2, reverse=1)
model.currentcollector(entity_type=ent.Component, name="mid2")
model.surfacemode(mode=1)
components_collection = hm.Collection(model, hm.FilterByEnumeration(ent.Component, ids=[4]))
collection = hm.Collection(model, hm.FilterByCollection(ent.Element, ent.Component), components_collection)
model.split_elements_by_structure_pattern_freeselection(collection=collection, refine_size=10, adjacent_layers=1)
entities = ent.Material(model, 5)
model.createentitysameas(entities=entities)
if __name__ == "__main__":
main()
import hm
import hm.entities as ent
model = hm.Model()
def main():
model.currentcollector(entity_type=ent.Component, name="center")
model.surfacemode(mode=4)
list1 = hm.EntityListByInteractiveSelection(model, ent.Node)
list2 = [ent.Node(model, id) for id in [12, 21, 22, 31]]
model.linearsurfacebetweennodes(list1=list1, list2=list2, reverse=1)
model.currentcollector(entity_type=ent.Component, name="mid2")
model.surfacemode(mode=1)
collection = hm.CollectionByInteractiveSelection(model, ent.Element)
model.split_elements_by_structure_pattern_freeselection(collection=collection, refine_size=10, adjacent_layers=1)
entities = hm.EntityByInteractiveSelection(model, ent.Material)
model.createentitysameas(entities=entities)
if __name__ == "__main__":
main()
Linking HyperMesh Parameter Entities#
HyperMesh Parameter entity provides a generic way of parametrizing different model attributes. They can be linked to the CAD data, to various solver attributes, or they can be linked to the arguments of the recorded functions.
Upon checking the box in the Link Parameter Entity column, the combobox in the Variables/Parameters column will be populated with a list of available parameter entities. Same filter is applied to parameters as to the variables based on the argument type.
Figure 5. Parameter entity “thickness_param” of type double.
Figure 6. Activating the parameter entity link and selecting “thickness_param” parameter.
import hm
import hm.entities as ent
model = hm.Model()
def main():
model.createsolverkeyword(keyword="PSHELL", type=ent.Property)
property = ent.Property(model, 1)
property.includeid = 0
property.PSHELL_T = ent.Parameter(model, 1).valuedouble
if __name__ == "__main__":
main()