Snippets
Edit meta data on entity
Using this action it is possible to edit and update meta data from the web interface.
Using the action
Navigate to a project in the web interface and select an entity in the spreadsheet and select Actions from the context menu. Click on Edit metadata.
In the window you will be presented with all available metadata on the entity and the possiblity to edit it. Edit a value and click submit. Once the data is persisted the window will display again with the updated values.
Available on
- Project
- Tasks (including encapsulating folders such as shots)
- Version
- Component
Running the action
Install the ftrack-action-handler:
pip install git+https://bitbucket.org/ftrack/ftrack-action-handler.git
Tip
You can use virtualenv or similar solutions to avoid installing packages into your system's Python environment.
Start the listener from the terminal using the following command:
python edit_meta_data_action.py
If you wish to see debugging information, set the verbosity level by appending -v debug to the command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | # :coding: utf-8
# :copyright: Copyright (c) 2015 ftrack
import sys
import argparse
import logging
import ftrack_api
import ftrack_action_handler.action
class EditMetaData(ftrack_action_handler.action.BaseAction):
'''Edit meta data action.'''
#: Action identifier.
identifier = 'ftrack.edit.meta.data'
#: Action label.
label = 'Edit metadata'
#: Action description.
description = 'Edit meta data on entity'
def validate_selection(self, session, entities):
'''Return if *entities* is a valid selection.'''
if (len(entities) != 1):
# If entities contains more than one item return early since
# metadata cannot be edited for several entites at the same time.
return False
entity_type, entity_id = entities[0]
if (
entity_type not in session.types or
'metadata' not in session.types[entity_type].attributes.keys()
):
# Return False if the target entity does not have a metadata
# attribute.
return False
return True
def discover(self, session, entities, event):
'''Return True if action is valid.'''
self.logger.info('Got selection: {0}'.format(entities))
return self.validate_selection(session, entities)
def interface(self, session, entities, event):
'''Return interface.'''
values = event['data'].get('values', {})
if not values:
items = []
entity_type, entity_id = entities[0]
entity = session.get(entity_type, entity_id)
# Refresh metadata collection.
del entity['metadata']
session.populate(entity, 'metadata.key, metadata.value')
for key, value in entity['metadata'].items():
items.append({
'label': key,
'type': 'textarea',
'name': key,
'value': value
})
if not items:
items.append({
'type': 'label',
'value': '*No metadata found on entity.*'
})
return items
def launch(self, session, entities, event):
'''Launch edit meta data action.'''
try:
input_values = event['data']['values']
except KeyError:
input_values = None
entity_type, entity_id = entities[0]
entity = session.get(entity_type, entity_id)
if input_values:
for key, value in input_values.items():
if entity['metadata'][key] != value:
# Only update if changed.
entity['metadata'][key] = value
try:
session.commit()
except:
# Commit failed, rollback session and re-raise.
session.rollback()
raise
return True
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
action_handler = EditMetaData(session)
action_handler.register()
def main(arguments=None):
'''Set up logging and register action.'''
if arguments is None:
arguments = []
parser = argparse.ArgumentParser()
# Allow setting of logging level from arguments.
loggingLevels = {}
for level in (
logging.NOTSET, logging.DEBUG, logging.INFO, logging.WARNING,
logging.ERROR, logging.CRITICAL
):
loggingLevels[logging.getLevelName(level).lower()] = level
parser.add_argument(
'-v', '--verbosity',
help='Set the logging output verbosity.',
choices=loggingLevels.keys(),
default='info'
)
namespace = parser.parse_args(arguments)
# Set up basic logging
logging.basicConfig(level=loggingLevels[namespace.verbosity])
session = ftrack_api.Session()
register(session)
# Wait for events
logging.info(
'Registered actions and listening for events. Use Ctrl-C to abort.'
)
session.event_hub.wait()
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))
|
You can clone a snippet to your computer for local editing. Learn more.