Snippets
Created by
Milan Kolar
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 | # :coding: utf-8
import sys
import argparse
import logging
import getpass
import ftrack
class customAction(ftrack.Action):
'''Custom action.'''
#: Action identifier.
identifier = 'name.of.action'
#: Action label.
label = 'Name of Action'
#: Action Icon.
icon = "https://some.online.accessible.image.to.use.as.icon"
def __init__(self):
'''Initialise action handler.'''
self.logger = logging.getLogger(
__name__ + '.' + self.__class__.__name__
)
def register(self):
'''Register action.'''
ftrack.EVENT_HUB.subscribe(
'topic=ftrack.action.discover and source.user.username={0}'.format(
getpass.getuser()
),
self.discover
)
ftrack.EVENT_HUB.subscribe(
'topic=ftrack.action.launch and source.user.username={0} '
'and data.actionIdentifier={1}'.format(
getpass.getuser(), self.identifier
),
self.launch
)
def validateSelection(self, selection):
'''Return true if the selection is valid.
'''
if selection:
return False
return False
def discover(self, event):
'''Return action config if triggered on asset versions.'''
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 not self.validateSelection(selection):
return
return {
'items': [{
'label': self.label,
'actionIdentifier': self.identifier,
'icon': self.icon
}]
}
def launch(self, event):
'''Callback method for action.'''
selection = event['data'].get('selection', [])
self.logger.info(u'Launching action with selection {0}'.format(
selection))
######################################################################
# Implement your action logic here
######################################################################
return {
'success': True,
'message': 'Created job for updating thumbnails!'
}
def register(registry, **kw):
'''Register action. Called when used as an event plugin.'''
logging.basicConfig(level=logging.INFO)
action = customAction()
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)
'''Register action and listen for events.'''
logging.basicConfig(level=loggingLevels[namespace.verbosity])
ftrack.setup()
action = customAction()
action.register()
ftrack.EVENT_HUB.wait()
if __name__ == '__main__':
raise SystemExit(main(sys.argv[1:]))
|
Comments (0)
You can clone a snippet to your computer for local editing. Learn more.