Issue with additional_filter_presets using api

Hi,

Noticed this today that I was getting unexpected result back from using additional_filter_presets, was wondering if I was doing something wrong, or anyone else has noticed the same?

‘VERSION_NUMBER’: When dealing with multiple PublishedFiles associated with a group of entities, this returns only the PublishedFile with the highest version_number.

engine = sgtk.platform.current_engine()
tk, sg = engine.sgtk, engine.sgtk.shotgun

result = sg.find('PublishedFile',
    [
        ['task', 'is', engine.context.task]
    ],
    ['version_number'],
    additional_filter_presets = [
        {
            "preset_name": "LATEST",
            "latest_by": "VERSION_NUMBER"
        }
    ]
)

for published_file in result:
    print(published_file)
{'type': 'PublishedFile', 'id': 36204, 'version_number': 3}
{'type': 'PublishedFile', 'id': 36206, 'version_number': 4}
{'type': 'PublishedFile', 'id': 37799, 'version_number': 2}
{'type': 'PublishedFile', 'id': 38083, 'version_number': 18}
{'type': 'PublishedFile', 'id': 38084, 'version_number': 18}
{'type': 'PublishedFile', 'id': 38140, 'version_number': 23}
{'type': 'PublishedFile', 'id': 38154, 'version_number': 24}
{'type': 'PublishedFile', 'id': 38155, 'version_number': 24}
{'type': 'PublishedFile', 'id': 38382, 'version_number': 8}
{'type': 'PublishedFile', 'id': 38622, 'version_number': 9}
{'type': 'PublishedFile', 'id': 38626, 'version_number': 9}
{'type': 'PublishedFile', 'id': 38627, 'version_number': 9}
{'type': 'PublishedFile', 'id': 38635, 'version_number': 10}
{'type': 'PublishedFile', 'id': 39127, 'version_number': 12}

using find_one returns {‘type’: ‘PublishedFile’, ‘id’: 36204, ‘version_number’: 3}

Please give this a try:

engine = sgtk.platform.current_engine()
tk, sg = engine.sgtk, engine.sgtk.shotgun

result = sg.find('PublishedFile',
    [
        ['task', 'is', engine.context.task]
    ],
    [
        'version_number',
        'name',
        'code',
        'published_file_type.PublishedFileType.code',
    ],
    additional_filter_presets = [
        {
            "preset_name": "LATEST",
            "latest_by": "VERSION_NUMBER"
        }
    ]
)

for published_file in result:
    print(published_file)

I get the same output as before, with id 36204 being first in the list. Does this return highest version_number on your end?

Sorry, I didn’t explain that clearly enough.

To start with, Flow PT treats publishes that share the same link, task, name, and published_file_type as the same file—just different versions of it.

The issue here is that filtering for only the latest version among files grouped by these rules is quite a hassle.

additional_filter_presets is used to solve this. In other words, it isn’t just about picking the item with the highest version number overall; it’s about selecting the one with the highest version number from among the multiple publishes that are considered the same file based on the rules mentioned above.

To put it more simply, the results returned when using additional_filter_presets are identical to what you get in the Flow PT Web UI’s published file list when the “Latest” checkbox is selected.

This means that if any of the link, task, name, or published_file_type values ​​differ, multiple publishes will still be returned even when using additional_filter_presets, and their version numbers might not be sequential or could even overlap.

Since the code you initially provided only checks the version_number, it appears to be functioning correctly from my perspective.

The code I provided checks the name and published_file_type as well. Consequently, if multiple publishes with the same name and type are returned, that would be considered an “unexpected result.”

Additionally, if the results remain unchanged regardless of whether additional_filter_presets is used or not, that could also be considered an “unexpected result.”

If you simply want to find the highest version_number, I recommend using sort order instead of a filter:)

engine = sgtk.platform.current_engine()
tk, sg = engine.sgtk, engine.sgtk.shotgun

result = sg.find('PublishedFile',
    [
        ['task', 'is', engine.context.task]
    ],
    ['version_number'],
    order = [
        {
            "field_name": "version_number", 
            "direction": "desc",
        }
    ]
)

for published_file in result:
    print(published_file)

Thank you for the clarification. That makes sense, I appreciate you taking the time to explain it.