find_objs#

ENS_GLOBALS.find_objs(list: ENS_GROUP | list = [], filter: dict | None = None, group: int = 0, recurse: int = 0, depth: int = -1, types: list | None = None) ENS_GROUP | list#

Search an object (tree) for objects that match a specific criteria.

This method walks an array of objects or an ENS_GROUP rooted object tree, looking for objects that meet the specified filters. The object tree to be searched is passed via ‘list’, which can be a Python list or a ENS_GROUP subclass. The method will walk all of the objects in the list and all the children in the groups.

Once the algorithm finds an object, it will apply filter. The first filter is the ‘types’ list. This specifies a list of allowable type objects (e.g. ensight.objs.ENS_PART). Only objects of the types in the list are returned. Second, is the ‘filter’ dictionary. This is a dictionary keyed by attribute enums. The algorithm gets the attribute value for the specified enum (if the enum does not exist, the object is not selected) and compares it to the value of the key in the dictionary. If the value comparisons fail, the object is not selected. Note: the algorithm uses the cmp() method on the values in the dictionary to make the comparison. Thus, comparisons other than equality can be implemented using objects with custom cmp() methods.

By default, the routine returns a Python list of objects, but group=1 can be specified to return a user selection group object containing the objects that would be in the list. The group option makes it easy to change attributes in bulk on the objects. It also makes it possible to access a ‘ensobjlist’ interface (group.CHILDREN is an ‘ensobjlist’ instance) which includes a useful ‘find()’ method as well. The depth option controls the depth to which the method is allowed to recurse. A value of -1 puts no limits on the recursion.

Args:
list:

A list of objects or an ENS_GROUP instance.

filter:

A dictionary created by ENSOBJ attributes as key and example attribute values as the dictionary values. For example, filter=dict(VISIBLE=True) will select only objects that have a VISIBLE attribute with the value True. If no dictionary is provided, the filter check is not applied.

group:

By default, return a list of matching objects. If group is set to 1, return an ENS_GROUP instance instead. This can be useful for chaining operations, but it is not functional in the pyensight interface.

recurse:

By default, only the objects in ‘list’ (or the children of an ENS_GROUP) are checked. If recurse is set to 1, all objects supporting the CHILDREN attribute will be recursively checked as well.

depth:

This can be used to limit the depth of recursion (if recurse is set to 1). The default value (-1) will not limit recursion at all. Otherwise, the maximum depth of the search is limited to the value of this keyword.

types:

This is set to a list of class objects that the search is limited to. If unset, all classes are allowed.

Returns:

A list or ENS_GROUP object of the matched objects.

The find_objs() method can be used in a large number of ways. Some common examples are illustrated here. Note also that: ensight.objs.core.find_objs(…,group=1).CHILDREN will return an ensobjlist containing the objects returned by find_objs().

Examples:
# find all PART objects in the current case
l = ensight.objs.core.find_objs(ensight.objs.core.CURRENTCASE,
                                types=[ensight.objs.ENS_PART], recurse=1)

# find all non VISIBLE parts as a group object
g = ensight.objs.core.find_objs(core.PARTS,
                                filter={ensight.objs.enums.VISIBLE: False}, group=1)
# and make them all visible
g.setchildattr(ensight.objs.enums.VISIBLE, True)

# find all the VISIBLE, CLIP parts currently selected
d = {ensight.objs.enums.VISIBLE: True, ensight.objs.enums.PARTTYPE: ensight.PART_CLIP_PLANE}
l = ensight.objs.core.find_objs(ensight.objs.core.selection(), filter=d)

# define a class with a custom __cmp__ method
class strstr():
    def __init__(self, value):
        self._v = value

    def __cmp__(self, other):
        if other.find(self._v) >= 0:
            return 0
        return 1

    def __eq__(self, other):
        return other.find(self._v) >= 0

    def __ne__(self, other):
        return other.find(self._v) < 0

    def __lt__(self, other):
        return False

    def __gt__(self, other):
        return False

    def __le__(self, other):
        return other.find(self._v) >= 0

    def __ge__(self, other):
        return other.find(self._v) >= 0

# find all parts in the current case that have "Block" in their DESCRIPTION
# find_objs() will only use the __eq__() method, the others are there for completeness
d = {ensight.objs.enums.DESCRIPTION: strstr("Block")}
l = ensight.objs.core.find_objs(ensight.objs.core.CURRENTCASE, filter=d,
                                types=[ensight.objs.ENS_PART], recurse=1)

# define a pure comparison case to see if a variable is valid for a case
class casevar():
    def __cmp__(self, other):
        if other[ensight.objs.core.CURRENTCASE[0].CASENUMBER]:
            return 0
        return 1

    def __eq__(self, other):
        return other[ensight.objs.core.CURRENTCASE[0].CASENUMBER]

    def __ne__(self, other):
        return not other[ensight.objs.core.CURRENTCASE[0].CASENUMBER]

    def __lt__(self, other):
        return False

    def __gt__(self, other):
        return False

    def __le__(self, other):
        return other[ensight.objs.core.CURRENTCASE[0].CASENUMBER]

    def __ge__(self, other):
        return other[ensight.objs.core.CURRENTCASE[0].CASENUMBER]

# find active, scalar variables defined for the current case
d = {ensight.objs.enums.EXIST_CASE: casevar(), ensight.objs.enums.ACTIVE: 1,
     ensight.objs.enums.VARTYPE: ensight.objs.enums.ENS_VAR_SCALAR}
g = ensight.objs.core.find_objs(ensight.objs.core.VARIABLES, filter=d)

# Define a class that defines __cmp__ to be the "find" operation on a list
class multival():
    def __init__(self,lst):
        self._list = lst

    def __cmp__(self,other):
        if self._list.count(other):
            return 0
        return 1

    def __eq__(self, other):
        return self._list.count(other) > 0

    def __ne__(self, other):
        return self._list.count(other) <= 0

    def __gt__(self, other):
        return False

    def __lt__(self, other):
        return False

    def __ge__(self, other):
        return self._list.count(other) > 0

    def __le__(self, other):
        return self._list.count(other) > 0

# use it to find the parts with descriptions that are in a list
f = {ensight.objs.enums.DESCRIPTION: multival(["engine","windshields"])}
parts = ensight.objs.core.find_objs(ensight.objs.core.PARTS, filter=f)