+def set_bid(session, event):
+ if not event['data']['entities'][0]['entityType'] == 'task':
+ # We deal just with tasks.
+ if not event['data']['entities'][0]['action'] == 'update':
+ # We deal just with task updates.
+ task = event['data']['entities'][0]
+ task_changes = task['changes']
+ if not task_changes.get('typeid'):
+ # We are interested only in type changes.
+ task_id = task['entityId']
+ new_task_name = task_changes['name']['new']
+ task_bid = bid_mapping.get(new_task_name)
+ # http://ftrack-python-api.rtd.ftrack.com/en/stable/working_with_entities.html?highlight=bid
+ with session.auto_populating(False):
+ task_object = session.get('Task', task_id)
+ task_object['bid'] = task_bid
+ print 'setting task {} bid to {}'.format(new_task_name, task_bid)
+def register(session, **kw):
+ '''Register event listener.'''
+ # Validate that session is an instance of ftrack_api.Session. If not,
+ # assume that register is being called from an incompatible API
+ # and return without doing anything.
+ if not isinstance(session, ftrack_api.Session):
+ handle_event = functools.partial(set_bid, session)
+ session.event_hub.subscribe('topic=ftrack.update', handle_event)
+if __name__ == '__main__':
+ logging.basicConfig(level=logging.INFO)
+ # Remember, in version version 2.0 of the ftrack-python-api the default
+ # behavior will change from True to False.
+ session = ftrack_api.Session(auto_connect_event_hub=True)
+ 'Registered actions and listening for events. Use Ctrl-C to abort.'
+ session.event_hub.wait()