Select Objects By Name

Goal

The following script examples enable you to select an object based on its name.

Code

# Goal: Get the first body whose name contains the specified number
def GetBodyByPartNumber(number):
    bodies = DataModel.Project.Model.Geometry.GetChildren(DataModelObjectCategory.Body, True)
    for body in bodies:
        if str(number) in body.Name:
            return body

    ExtAPI.Log.WriteWarning("No bodies were found containing  " + str(number))
    return None
 
# Goal: Get objects whose name contains the specified string
def GetObjectsByPartialName(name):
    object_list = []
    child_list = Tree.AllObjects
    for child in child_list:
        if name in child.Name:
            object_list.Add(child)

    return object_list

# Goal: Get the first Named Selection whose name contains the specified string
def GetNamedSelectionByName(name,AcceptPartial = False):
    try:
        NSs = ExtAPI.DataModel.Project.Model.NamedSelections.Children
    except:
        return None
    for ns in NSs:
        if ns.Name.ToLower() == name.ToLower():
            return ns
        if AcceptPartial and  (name.ToLower() in ns.Name.ToLower()):
            return ns
    ExtAPI.Log.WriteWarning("Named Selection not found: "+str(name))
    return None

# Goal: Get Named Selections whose name contains the specified string  
def GetNamedSelectionsByName(name,AcceptPartial = False):
    try:
        NSs = ExtAPI.DataModel.Project.Model.NamedSelections.Children
    except:
        return []
    if AcceptPartial:
        return [ns for ns in NSs if name.ToLower() in ns.Name.ToLower()]
    else:
        return [ns for ns in NSs if ns.Name.ToLower() == name.ToLower()]