Snippets

ftrack Action - Action Boilerplate (Legacy API)

You are viewing an old version of this snippet. View the current version.
Revised by Lucas Correia 255872f

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.

Running the action

Start the listener from the terminal using the following command:

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.

# :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(selection):
            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(selection):
            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.