Getting the body ID from a node ID in Mechanical scripting

Jimmy He
Jimmy He Member, Employee Posts: 19
First Anniversary 10 Comments Photogenic Name Dropper
✭✭✭

Just wanted to share a quick snippet that allows one to get the body ID (and potentially select the body and get information about the body) from a node ID using Mechanical scripting:

Happy scripting!

Comments

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 864
    500 Comments Photogenic Name Dropper Solution Developer Community of Practice Member
    ✭✭✭✭

    Code in attachement:

    my_node = meshData.NodeById( node_ID )
    Find out which body the node belongs to
    body_ID = my_node.BodyIds[0]
    body = DataModel.GetObjectById( body_ID )

  • Kev
    Kev Member Posts: 41
    5 Likes First Anniversary 10 Comments Name Dropper
    **
    edited August 2023

    Hi @Pernelle Marone-Hitz
    Hope you've been well.
    Is this only available in 2023 version?
    when I attempted to use the BodyIds attribute in Ansys 2022r2, i get an error message saying: 'Node' object has no attribute 'BodyIds'.
    I defined the mash as follows:

    analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
    dataSource = dpf.DataSources(analysis.ResultFileName)
    model=dpf.Model(dataSource)
    mesh=model.Mesh

    Node was defined as so:

    node=mesh.NodeById(23742)

    and here's the prompt:

    node.BodyIds[0]

    'Node' object has no attribute 'BodyIds'

    Just checking to see if I've missed a step.
    Is there another way to achieve this?

  • Kev
    Kev Member Posts: 41
    5 Likes First Anniversary 10 Comments Name Dropper
    **
    edited August 2023

    My appologies, just found your other post on this and saw my mistake.
    mesh needs to be defined like this:
    ExtAPI.DataModel.Project.Model.Analyses[0].MeshData

    Thanks for this useful post Pernelle.
    Cheers :)