Dynamic enumerator

A dynamic enumerator is a custom attribute type that will fetch its values from a remote service. Before the values are presented in a drop-down menu, they are queried from the service. The query includes information about the current context being edited which can be used to filter the values. The context includes information about the current entity and unsaved changes if there are any.

Example service

This service handles two different enumerators, product_category and product_name. product_name is filtered based on product_category. For test purposes it will also output two test values for any other dynamic enumerator.

import ftrack


def myCallback(event):
    '''Dynamic enumerator service callback.

    event['data'] is expected to contain:

        * attributeName - The name of the custom attribute.
        * recordData - Information about changes to the entity.

    '''
    changes = event['data']['recordData'].get('changes', {})
    attributeName = event['data']['attributeName']

    output = []
    if attributeName == 'product_category':
        # Handle product_category.
        output = [
            {
                'name': 'Furniture',
                'value': 'furniture'
            }, {
                'name': 'Car',
                'value': 'car'
            }
        ]

    elif attributeName == 'product_name':
        # Handle product_name based on product_category. This will only work
        # if product_category is passed with changes, which will happen when
        # creating projects for example. product_category could also be
        # queried using the API.

        selectedProductCategory = changes.get('product_category')

        if selectedProductCategory == 'furniture':
            output = [{
                'name': 'Chair',
                'value': 'chair'
            }, {
                'name': 'Table',
                'value': 'table'
            }]

        elif selectedProductCategory == 'car':
            output = [{
                'name': 'Saab',
                'value': 'saab'
            }, {
                'name': 'Volvo',
                'value': 'volvo'
            }]

    else:
        # Output some test data.
        output = [{
            'name': 'Test A',
            'value': 'test_a'
        }, {
            'name': 'Test B',
            'value': 'test_b'
        }]

    return output


# Subscribe to the dynamic enumerator topic.
ftrack.setup()
ftrack.EVENT_HUB.subscribe('topic=ftrack.dynamic-enumerator', myCallback)
ftrack.EVENT_HUB.wait()