Snippets

ftrack API Example - Get hierarchical attribute

Updated by Mattias Lagergren

File get_hierarchical_attribute.py Modified

  • Ignore whitespace
  • Hide word diff
 # :coding: utf-8
 # :copyright: Copyright (c) 2019 ftrack
 
+
 def get_hierarchical_attribute(
     session,
-    typed_context,
-    attribute_name,
-    default=None
+    entity,
+    attribute_name
 ):
-    '''Return hierarchical attribute *attribute_name* or *default*.'''
-    # Prefetch required attributes and construct ordered list of ancestors.
-    session.query(
-        'select ancestors.custom_attribute, project.custom_attribute '
-        'from TypedContext where id is "{}"'.format(typed_context['id'])
-    )
-    ancestors = (
-        list(reversed(typed_context['ancestors'])) +
-        [typed_context['project']]
-    )
+    '''Return hierarchical attribute *attribute_name* or default.'''
+    entities = []
+    if isinstance(entity, session.types['Project']):
+        entities = [entity]
+    else:
+        if isinstance(entity, session.types['AssetVersion']):
+            asset_version = session.query(
+                'select asset.parent.ancestors.id, asset.parent.project ' +
+                'from AssetVersion where id is "{}"'.format(entity['id'])
+            ).one()
+            parent = asset_version['asset']['parent']
+            if isinstance(parent, session.types['Project']):
+                entities = (
+                    [asset_version] +
+                    [parent]
+                )
+            else:
+                entities = (
+                    [asset_version] +
+                    [parent] +
+                    list(reversed(parent['ancestors'])) +
+                    [parent['project']]
+                )
+        else:
+            typed_context = session.query(
+                'select ancestors.id, project ' +
+                'from TypedContext where id is "{}"'.format(entity['id'])
+            ).one()
+            entities = (
+                [typed_context] +
+                list(reversed(typed_context['ancestors'])) +
+                [typed_context['project']]
+            )
+
+    entity_ids = [item['id'] for item in entities]
+
+    [values, default_value] = session.call([{
+        'action': 'query',
+        'expression': (
+            'select value, entity_id from CustomAttributeValue '
+            'where entity_id in ({0}) and configuration.key is "{1}"'
+            .format(
+                ', '.join(
+                    ['"{0}"'.format(entity_id) for entity_id in entity_ids]
+                ),
+                attribute_name
+            )
+        )
+    }, {
+        'action': 'query',
+        'expression': (
+            'select default from CustomAttributeConfiguration '
+            'where key is "{0}"'.format(
+                attribute_name
+            )
+        )
+    }])
 
-    attribute_value = default
-    for context in ancestors:
-        if context['custom_attributes'][attribute_name]:
-            attribute_value = context['custom_attributes'][attribute_name]
-            break
+    attribute_value = default_value['data'][0]['default']
+    if values['data']:
+        attribute_value = sorted(
+            values['data'],
+            key=lambda value: entity_ids.index(value['entity_id'])
+        )[0]['value']
 
     return attribute_value
Created by Lucas Correia

File example_usage.py Added

  • Ignore whitespace
  • Hide word diff
+import ftrack_api
+import get_hierarchical_attribute from get_hierarchical_attribute
+
+session = ftrack_api.Session()
+task = session.query('Task').first()
+fps = get_hierarchical_attribute(session, task, 'fps')
+print 'FPS', fps

File get_hierarchical_attribute.py Added

  • Ignore whitespace
  • Hide word diff
+# :coding: utf-8
+# :copyright: Copyright (c) 2019 ftrack
+
+def get_hierarchical_attribute(
+    session,
+    typed_context,
+    attribute_name,
+    default=None
+):
+    '''Return hierarchical attribute *attribute_name* or *default*.'''
+    # Prefetch required attributes and construct ordered list of ancestors.
+    session.query(
+        'select ancestors.custom_attribute, project.custom_attribute '
+        'from TypedContext where id is "{}"'.format(typed_context['id'])
+    )
+    ancestors = (
+        list(reversed(typed_context['ancestors'])) +
+        [typed_context['project']]
+    )
+
+    attribute_value = default
+    for context in ancestors:
+        if context['custom_attributes'][attribute_name]:
+            attribute_value = context['custom_attributes'][attribute_name]
+            break
+
+    return attribute_value
HTTPS SSH

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