Single query for Assets and Shots problem

Hi,

I made a query to find a Task related to an asset whose name I have

sg_filters = [
        ["project", "is",  {'type': 'xxxxxx', 'id': 100, 'name': 'xxxxx'}],
        ["entity.Asset.code", "is", "ABC"],
            ]
fields = ["id", "type", "content", "entity", "step"]
sg_data = shotgun.find_one("Task", filters=sg_filters, fields=fields)

This works, but I would like this to work also if the Task is related to a Shot. I can do a second query using :

...
      ["entity.Asset.code", "is", "ABC"],
...

But of course, I’d prefer to have a single query for both cases. Is this possible ?

Regards

Hey @donat

You can perform a query using a complex filter that asks to use “any” filters within a group of filters. Using your example:


filters = []
# Common filters that will always be applicable
filters.append(["project", "is",  {'type': 'xxxxxx', 'id': 100, 'name': 'xxxxx'}])
filters.append(["entity", "name_is", "ABC"]  # Same as entity.EntityType.code but more convenient and omits the need to specify the Entity type explicitly. This alone may be an answer to your question.

# Filters that are optional but results must match at least 1 of these filters
any_filter_group = {
    "filter_operator": "any",
    "filters": []
}
# Request the linked Entity is either a Shot or an Asset
any_filter_group["filters"].append(["entity", "type_is", "Asset"])
any_filter_group["filters"].append(["entity", "type_is", "Shot"])

filters.append(any_filter_group)

fields = ["id", "type", "content", "entity", "step"]

sg_data = shotgun.find_one("Task", filters=sg_filters, fields=fields)
2 Likes

Works perfectly, thanks a lot Halil!