# :copyright: Copyright (c) 2019 ftrack
def get_hierarchical_attribute(
- '''Return hierarchical attribute *attribute_name* or *default*.'''
- # Prefetch required attributes and construct ordered list of ancestors.
- 'select ancestors.custom_attribute, project.custom_attribute '
- 'from TypedContext where id is "{}"'.format(typed_context['id'])
- list(reversed(typed_context['ancestors'])) +
- [typed_context['project']]
+ '''Return hierarchical attribute *attribute_name* or default.'''
+ if isinstance(entity, session.types['Project']):
+ 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'])
+ parent = asset_version['asset']['parent']
+ if isinstance(parent, session.types['Project']):
+ list(reversed(parent['ancestors'])) +
+ typed_context = session.query(
+ 'select ancestors.id, project ' +
+ 'from TypedContext where id is "{}"'.format(entity['id'])
+ list(reversed(typed_context['ancestors'])) +
+ [typed_context['project']]
+ entity_ids = [item['id'] for item in entities]
+ [values, default_value] = session.call([{
+ 'select value, entity_id from CustomAttributeValue '
+ 'where entity_id in ({0}) and configuration.key is "{1}"'
+ ['"{0}"'.format(entity_id) for entity_id in entity_ids]
+ 'select default from CustomAttributeConfiguration '
+ 'where key is "{0}"'.format(
- attribute_value = default
- for context in ancestors:
- if context['custom_attributes'][attribute_name]:
- attribute_value = context['custom_attributes'][attribute_name]
+ attribute_value = default_value['data'][0]['default']
+ attribute_value = sorted(
+ key=lambda value: entity_ids.index(value['entity_id'])