Snippets

ftrack set bid on task type change

Updated by Lorenzo Angeli

File update_bid_on_task_type_change.py Modified

  • Ignore whitespace
  • Hide word diff
 import functools
 
 bid_mapping = {
-    'texturing': 5,
-    'lookdev': 4,
-    'modeling': 100
+    'Texturing': 600000,
+    'Lookdev': 600000,
+    'Modeling': 600000
 }
 
 
         # We are interested only in type changes.
         return
 
+    task_type = session.get('Type', task_changes['typeid']['new'])
+    task_type_name = task_type['name']
+    task_bid = bid_mapping.get(task_type_name)
     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)
+        print 'setting task {} bid to {}'.format(task_type_name, task_bid)
     
     session.commit()
 
Updated by Lorenzo Angeli

File update_bid_on_task_type_change.py Modified

  • Ignore whitespace
  • Hide word diff
         # We are interested only in type changes.
         return
 
-    print task_changes
-
     task_id = task['entityId']
     new_task_name = task_changes['name']['new']
 
Created by Lorenzo Angeli

File update_bid_on_task_type_change.py Added

  • Ignore whitespace
  • Hide word diff
+import logging
+import ftrack_api
+import functools
+
+bid_mapping = {
+    'texturing': 5,
+    'lookdev': 4,
+    'modeling': 100
+}
+
+
+def set_bid(session, event):
+    if not event['data']['entities'][0]['entityType'] == 'task':
+        # We deal just with tasks.
+        return
+
+    if not event['data']['entities'][0]['action'] == 'update':
+        # We deal just with task updates.
+        return
+
+    task = event['data']['entities'][0]
+    task_changes = task['changes']
+
+    if not task_changes.get('typeid'):
+        # We are interested only in type changes.
+        return
+
+    print task_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)
+    
+    session.commit()
+
+
+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):
+        return
+
+    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)
+    register(session)
+    logging.info(
+        'Registered actions 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.