Snippets

ftrack Action - Create notes on multiple entities

Updated by Lucas Correia

File README.rst Modified

  • Ignore whitespace
  • Hide word diff
 
 Start the listener from the terminal using the following command:
 
-.. code:
+.. code::
 
     python note_on_multiple_entities_action.py
 
 If you wish to see debugging information, set the verbosity level by appending
-"-v debug" to the command.
+``-v debug`` to the command.
 
 For more information, see the 
 `documentation on using actions <http://ftrack.rtd.ftrack.com/en/latest/using/actions.html>`_.
 ================
 
 Navigate to a project in the web interface and select a few items
-in the spreadsheet and select :guilabel:`Actions` from the context menu. Click 
-on :guilabel:`Write notes` and add your text and optionally select a note
+in the spreadsheet and select `Actions` from the context menu. Click 
+on `Write notes` and add your text and optionally select a note
 category.
 
-.. note:
-
-    The notes created are copies, and any replies will only show up on one of
-    the notes.
+Notes created are copies, and any replies will only show up on one of the notes.
Updated by Lucas Correia

File README.rst Added

  • Ignore whitespace
  • Hide word diff
+..
+    :copyright: Copyright (c) 2015 ftrack
+
+********************************
+Note on multiple entities action
+********************************
+
+Running the action
+==================
+
+Start the listener from the terminal using the following command:
+
+.. code:
+
+    python note_on_multiple_entities_action.py
+
+If you wish to see debugging information, set the verbosity level by appending
+"-v debug" to the command.
+
+For more information, see the 
+`documentation on using actions <http://ftrack.rtd.ftrack.com/en/latest/using/actions.html>`_.
+
+Using the action
+================
+
+Navigate to a project in the web interface and select a few items
+in the spreadsheet and select :guilabel:`Actions` from the context menu. Click 
+on :guilabel:`Write notes` and add your text and optionally select a note
+category.
+
+.. note:
+
+    The notes created are copies, and any replies will only show up on one of
+    the notes.
Created by Lucas Correia

File note_on_multiple_entities_action.py Added

  • Ignore whitespace
  • Hide word diff
+# :coding: utf-8
+# :copyright: Copyright (c) 2015 ftrack
+import sys
+import argparse
+import logging
+import threading
+
+import ftrack
+
+
+def async(fn):
+    '''Run *fn* asynchronously.'''
+    def wrapper(*args, **kwargs):
+        thread = threading.Thread(target=fn, args=args, kwargs=kwargs)
+        thread.start()
+    return wrapper
+
+
+class NoteOnMultipleEntitiesAction(ftrack.Action):
+    '''Action to write note on multiple entities.'''
+
+    #: Action identifier.
+    identifier = 'note-on-multiple-entities'
+
+    #: Action label.
+    label = 'Write notes'
+
+    @async
+    def createNotes(self, selection, text, category):
+        entityCount = len(selection)
+        logging.info('Creating notes on {0} entities'.format(entityCount))
+
+        job = ftrack.createJob(
+            'Creating notes ({0} of {1})'.format(1, entityCount), 'running'
+        )
+        try:
+            for index, item in enumerate(selection, start=1):
+                entityType = item['entityType']
+                entityId = item['entityId']
+                entity = None
+
+                if index != 1:
+                    job.setDescription('Creating notes ({0} of {1})'.format(index, entityCount))
+
+                if entityType == ftrack.Project._type:
+                    entity = ftrack.Project(entityId)
+                elif entityType == ftrack.Task._type:
+                    entity = ftrack.Task(entityId)
+                elif entityType == ftrack.AssetVersion._type:
+                    entity = ftrack.AssetVersion(entityId)
+
+                if not entity:
+                    logging.warning(
+                        u'Entity ({0}, {1}) not a valid type, skipping..'
+                        .format(entityId, entityType)
+                    )
+
+                entity.createNote(text, category)
+        except Exception:
+            job.setStatus('failed')
+
+        logging.info('Note creation completed.')
+        job.setStatus('done')
+
+    def launch(self, event):
+        '''Callback method for action.'''
+        data = event['data']
+        logging.info(u'Launching action with data: {0}'.format(data))
+
+        selection = data.get('selection', [])
+        if not selection:
+            return {'success': False}
+
+        if 'values' in data:
+            text = data['values'].get('note_text')
+            category = data['values'].get('note_category', 'auto')
+            self.createNotes(selection, text, category)
+
+            return {
+                'success': True,
+                'message': 'Started creating notes'
+            }
+
+        options = [
+            {'label': category.getName(), 'value': category.getId()}
+            for category in ftrack.getNoteCategories()
+        ]
+        options.insert(0, {'label': 'Default', 'value': 'auto'})
+
+
+        return {
+            'items': [
+                {
+                    'value': '## Writing note on **{0}** items. ##'.format(len(selection)),
+                    'type': 'label'
+                }, {
+                    'label': 'Content',
+                    'name': 'note_text',
+                    'value': '',
+                    'type': 'textarea'
+                }, {
+                    'label': 'Note category',
+                    'type': 'enumerator',
+                    'name': 'note_category',
+                    'value': 'auto',
+                    'data': options
+                }
+            ]
+        }
+
+
+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])
+
+    # Subscribe to action.
+    ftrack.setup()
+    action = NoteOnMultipleEntitiesAction()
+    action.register()
+
+    # Wait for events
+    ftrack.EVENT_HUB.wait()
+
+
+if __name__ == '__main__':
+    raise SystemExit(main(sys.argv[1:]))
  1. 1
  2. 2
HTTPS SSH

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