Snippets

ftrack API Example - Dynamic enumerator using API to fetch options

Created by Lucas Correia

File dynamic_enumerator.py Added

  • Ignore whitespace
  • Hide word diff
+# :coding: utf-8
+# :copyright: Copyright (c) 2019 ftrack
+import functools
+import logging
+
+import ftrack_api
+
+
+logger = logging.getLogger(__name__)
+
+
+def callback(session, event):
+    '''Dynamic enumerator callback.'''
+    logger.info(u'Running callback for event: {}'.format(event))
+    attribute_name = event['data']['attributeName']
+
+    if attribute_name == 'asset_build':
+        output = []
+        try:
+            entityId = event['data']['recordData']['entity']['entityId']
+            # Query AssetBuild entities on the same project as the entity being
+            # edited.
+            asset_builds = session.query(
+                'select name from AssetBuild where project_id in (select project_id from TypedContext where id is "{}")'.format(
+                    entityId
+                )
+            )
+            output = [
+                { 'name': item['name'], 'value': item['name'] }
+                for item in asset_builds
+            ]
+        except Exception:
+            logger.exception('Failed to get options')
+
+        return output
+
+
+def register(session, **kw):
+    '''Register plugin. Called when used as an plugin.'''
+    # Validate that session is an instance of ftrack_api.Session. If not,
+    # assume that register is being called from an old or incompatible API and
+    # return without doing anything.
+    if not isinstance(session, ftrack_api.session.Session):
+        return
+
+    session.event_hub.subscribe(
+        'topic=ftrack.dynamic-enumerator', functools.partial(callback, session)
+    )
+
+
+if __name__ == '__main__':
+    logging.basicConfig(level=logging.INFO)
+    session = ftrack_api.Session()
+    register(session)
+
+    # Wait for events
+    logging.info('Registered event listener and listening for events. Use Ctrl-C to abort.')
+    session.event_hub.wait()
+
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.