Add and edit materials using PyMechanical

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

  • Member, Moderator, Employee Posts: 323
    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:

    1. materialss = Model.Materials
    2. materialss.Import(material_file) # material file is the xml with materials
    3. matAssignment = materialss.AddMaterialAssignment()
    4. tempSel = ExtAPI.SelectionManager.CreateSelectionInfo(Ansys.ACT.Interfaces.Common.SelectionTypeEnum.GeometryEntities)
    5. bodies = [body for body in ExtAPI.DataModel.Project.Model.Geometry.GetChildren(Ansys.Mechanical.DataModel.Enums.DataModelObjectCategory.Body,True)]
    6. geobodies = [body.GetGeoBody() for body in bodies]
    7. ids = System.Collections.Generic.List[System.Int32]()
    8. [ids.Add(item.Id) for item in geobodies]
    9. tempSel.Ids = ids
    10. matAssignment.Location = tempSel
    11. 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

Welcome!

It looks like you're new here. Sign in or register to get started.