I would like a script that takes the name of a NamedSelection as input and returns the NamedSelection
def GetNSbyName(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
This function:
def ns_by_name(ns_name): return [o for o in DataModel.GetObjectsByType(DataModelObjectCategory.NamedSelection) if o.Name == ns_name]
ns_by_name('name')[0]
Hi @Landon Mitchell Kanner Can you include the code in your post? It is best practice to avoid just linking to external resources because the answer is quite fragile and depends on the link's persistence, as well as user access. For example, if that GitHub repo goes private next week, then people won't be able to check out GetNSbyName.
Combining both answers (untested):
def GetNSbyName(name,AcceptPartial = False): NSs = DataModel.GetObjectsByType(DataModelObjectCategory.NamedSelection) 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()]