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.
# :coding: utf-8# :copyright: Copyright (c) 2015 ftrackimportsysimportargparseimportloggingimportftrackclassMyAction(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.deflaunch(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 validifnotself.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.passreturn{'success':True,'message':'Action completed successfully'}defdiscover(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.ifself.validateSelection(selection):returnsuper(MyAction,self).discover(event)defvalidateSelection(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.returnTruedefregister(registry,**kw):'''Register action. Called when used as an event plugin.'''action=MyAction()action.register()defmain(arguments=None):'''Set up logging and register action.'''ifargumentsisNone:arguments=[]parser=argparse.ArgumentParser()# Allow setting of logging level from arguments.loggingLevels={}forlevelin(logging.NOTSET,logging.DEBUG,logging.INFO,logging.WARNING,logging.ERROR,logging.CRITICAL):loggingLevels[logging.getLevelName(level).lower()]=levelparser.add_argument('-v','--verbosity',help='Set the logging output verbosity.',choices=loggingLevels.keys(),default='info')namespace=parser.parse_args(arguments)# Set up basic logginglogging.basicConfig(level=loggingLevels[namespace.verbosity])# Subscribe to action.ftrack.setup()action=MyAction()action.register()# Wait for eventsftrack.EVENT_HUB.wait()if__name__=='__main__':raiseSystemExit(main(sys.argv[1:]))
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.