Add and edit materials using PyMechanical

Benjam
Benjam Member Posts: 1 **

Dear all,
I'm trying to run a script that build, solve and process a system using PyMechanical.
I'm stuck trying to create and edit material. Is it possible without using Workbench ?
Regards

Comments

  • Erik Kostson
    Erik Kostson Member, Moderator, Employee Posts: 327
    50 Answers 100 Comments Second Anniversary 25 Likes
    ✭✭✭✭
    edited March 28

    Hello Benjam

    Welcome to the forum.

    We can import a material data xml file, and assign materials as needed to parts using the below:

    materialss = Model.Materials
    materialss.Import(material_file) # material file is the xml with materials
    matAssignment = materialss.AddMaterialAssignment()
    tempSel = ExtAPI.SelectionManager.CreateSelectionInfo(Ansys.ACT.Interfaces.Common.SelectionTypeEnum.GeometryEntities)
    bodies = [body for body in ExtAPI.DataModel.Project.Model.Geometry.GetChildren(Ansys.Mechanical.DataModel.Enums.DataModelObjectCategory.Body,True)]
    geobodies = [body.GetGeoBody() for body in bodies]
    ids = System.Collections.Generic.List[System.Int32]()
    [ids.Add(item.Id) for item in geobodies]
    tempSel.Ids = ids
    matAssignment.Location = tempSel
    matAssignment.Material = "Aluminum Alloy"
    

    The above works in 2024 R2 (in 2023 R2 there was an issue / see here: https://discuss.ansys.com/discussion/2363/import-materials-in-pymechanical)

    Hope this helps to get started - to edit material one could for instance change perhaps a field (say density) inside the xml file directly, and reimport it, but perhaps other forum members have some more ideas( @AKD-Scripting-Team )

    All the best

    Erik