Snippets
Revised by
Lucas Correia
67a3f4a
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.
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 | # :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(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.
return True
def register(registry, **kw):
'''Register action. Called when used as an event plugin.'''
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):
'''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:]))
|
You can clone a snippet to your computer for local editing. Learn more.