Snippets

ftrack Action - Action Boilerplate (Legacy API)

Updated by Lucas Correia

File action_boilerplate.py Modified

  • Ignore whitespace
  • Hide word diff
             'ftrack.Registry instance.'.format(registry)
         )
         return
+    action = MyAction()
+    action.register()
 
 
 def main(arguments=None):
Updated by Lucas Correia

File action_boilerplate.py Modified

  • Ignore whitespace
  • Hide word diff
 
 def register(registry, **kw):
     '''Register action. Called when used as an event plugin.'''
-    action = MyAction()
-    action.register()
+    logger = logging.getLogger(
+        'com.example.my-action'
+    )
+
+    # Validate that registry is an instance of ftrack.Registry. If not,
+    # assume that register is being called from a new or incompatible API and
+    # return without doing anything.
+    if not isinstance(registry, ftrack.Registry):
+        logger.debug(
+            'Not subscribing plugin as passed argument {0!r} is not an '
+            'ftrack.Registry instance.'.format(registry)
+        )
+        return
 
 
 def main(arguments=None):
Updated by Lucas Correia

File action_boilerplate.py Modified

  • Ignore whitespace
  • Hide word diff
         if self.validateSelection(selection):
             return super(MyAction, self).discover(event)
 
-    def validateSelection(selection):
+    def validateSelection(self, selection):
         '''Return True if *selection* is valid'''
         # Replace with custom logic for validating selection.
         # For example check the length or entityType of items in selection.
Updated by Lucas Correia

File action_boilerplate.py Modified

  • Ignore whitespace
  • Hide word diff
         self.logger.info(u'Launching action with selection {0}'.format(selection))
 
         # Validate selection and abort if not valid
-        if not self.validateSelection():
+        if not self.validateSelection(selection):
             self.logger.warning('Selection is not valid, aborting action')
             return
 
         self.logger.info(u'Discovering action with selection: {0}'.format(selection))
 
         # validate selection, and only return action if it is valid.
-        if self.validateSelection():
+        if self.validateSelection(selection):
             return super(MyAction, self).discover(event)
 
     def validateSelection(selection):
Created by Lucas Correia

File README.rst Added

  • Ignore whitespace
  • Hide word diff
+..
+    :copyright: Copyright (c) 2015 ftrack
+
+******************
+Action boilerplate
+******************
+
+You can use this boilerplate to get started quickly with Actions. Replace any
+references to MyAction in the code and add your custom logic. For more information
+about using actions, see see the 
+`documentation on using actions <http://ftrack.rtd.ftrack.com/en/latest/using/actions.html>`_.
+
+Running the action
+==================
+
+Start the listener from the terminal using the following command:
+
+.. code::
+
+    python action_boilerplate.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>`_.

File action_boilerplate.py Added

  • Ignore whitespace
  • Hide word diff
+# :coding: utf-8
+# :copyright: Copyright (c) 2015 ftrack
+
+import sys
+import argparse
+import logging
+
+import ftrack
+
+
+class MyAction(ftrack.Action):
+    '''Describe your action here.'''
+
+    #: Action identifier.
+    identifier = 'com.example.my-action'  # Unique identifier for your action.
+
+    #: Action label.
+    label = 'My action'  # Action label which the user will see in the interface.
+
+    def launch(self, event):
+        '''Callback method for action.'''
+        selection = event['data'].get('selection', [])
+        self.logger.info(u'Launching action with selection {0}'.format(selection))
+
+        # Validate selection and abort if not valid
+        if not self.validateSelection():
+            self.logger.warning('Selection is not valid, aborting action')
+            return
+
+        # Implement your action logic here, return a UI or run some
+        # code and return your result.
+        # 
+        # If you are doing any heavy lifting, consider running offloading that
+        # to a separate thread and returning quickly.
+        # 
+        # To report back to the user you can utilize ftrack.createJob and then
+        # update it's status and / or description.
+        pass
+
+        return {
+            'success': True,
+            'message': 'Action completed successfully'
+        }
+
+    def discover(self, event):
+        '''Return action config.'''
+        selection = event['data'].get('selection', [])
+        self.logger.info(u'Discovering action with selection: {0}'.format(selection))
+
+        # validate selection, and only return action if it is valid.
+        if self.validateSelection():
+            return super(MyAction, self).discover(event)
+
+    def validateSelection(selection):
+        '''Return True if *selection* is valid'''
+        # Replace with custom logic for validating selection.
+        # For example check the length or entityType of items in selection.
+        return True
+
+
+def register(registry, **kw):
+    '''Register action. Called when used as an event plugin.'''
+    action = MyAction()
+    action.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])
+
+    # Subscribe to action.
+    ftrack.setup()
+    action = MyAction()
+    action.register()
+
+    # Wait for events
+    ftrack.EVENT_HUB.wait()
+
+if __name__ == '__main__':
+    raise SystemExit(main(sys.argv[1:]))
HTTPS SSH

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