Actions using ftrack_api
2 2

4 posts in this topic

Hello,so far I used legacy api to create some actions.Then using "FTRACK_EVENT_PLUGIN_PATH" I made them visible for all my team;

but lately I've been reading about the new api and tried to create an action using it.I found a sort of example  but I am having some trouble to make it work.

Please look at this and tell me what is wrong or what is missing:

ftrack_api.thumb.PNG.38178f6bec860d79f44

 

Share this post


Link to post
Share on other sites

here it is:

 

# :coding: utf-8
import logging
import uuid
import ftrack_api


class checkShots(object):
    """Custom action"""
    label = "Check Shot(s)"
    identifier = 'com.ftrack.{0}'.format(str(uuid.uuid1()))
    description = "Let you populate shots"
    
    def __init__(self, self.session):
        '''Initialise action handler.'''
        self.logger = logging.getLogger(
            __name__ + '.' + self.__class__.__name__
        )
        self.session = session
    
    
    def register(self):
        '''Register action.'''
        self.session.event_hub.subscribe(
            'topic=ftrack.action.discover',
            self.discover
        )
        self.session.event_hub.subscribe(
            'topic=ftrack.action.launch and data.actionIdentifier={0}'.format(
                self.identifier
            ),
            self.launch
        )
    
    def discover(self, event):
        '''Return action config.'''
        selection = event['data']['selection']

        # Need something to convert.
        if not selection:
            return
 
 
    def launch(self, event):        
        selection = event['data']['selection']
        if selection:
            return {
                "success": True,
                "message": "Looking good!"
            }



def main():
    logging.basicConfig(level=logging.INFO)
    session = ftrack_api.session()

    action = checkShots(session)
    action.register()
    
    session.event_hub.wait()

if __name__ == "__main__":
    main()

 

thanks!

Share this post


Link to post
Share on other sites

The main reason your action didn't work was because you weren't returning anything in the "discover" method. There was also some problems with syntax, and I've isolated the action to be only for the current user instead of everyone (look at the change in the "register" method). Have a look at the code below:

 

# :coding: utf-8
import logging
import uuid
import ftrack_api
import getpass


class checkShots(object):
    """Custom action"""
    label = "Check Shot(s)"
    identifier = 'com.ftrack.{0}'.format(str(uuid.uuid1()))
    description = "Let you populate shots"

    def __init__(self, session):
        '''Initialise action handler.'''
        self.logger = logging.getLogger(
            __name__ + '.' + self.__class__.__name__
        )
        self.session = session


    def register(self):
        '''Register action.'''
        self.session.event_hub.subscribe(
            'topic=ftrack.action.discover and source.user.username={0}'.format(
                getpass.getuser()
            ),
            self.discover
        )
        self.session.event_hub.subscribe(
            'topic=ftrack.action.launch and source.user.username={0} '
            'and data.actionIdentifier={1}'.format(
                getpass.getuser(), self.identifier
            ),
            self.launch
        )

    def discover(self, event):
        '''Return action config.'''

        selection = event['data']['selection']

        # Need something to convert.
        if not selection:
            return

        return {
            'items': [{
                'label': self.label,
                'actionIdentifier': self.identifier,
                'icon': "http://a.fsdn.com/allura/p/djv/icon"
            }]
        }


    def launch(self, event):
        selection = event['data']['selection']
        if selection:
            return {
                "success": True,
                "message": "Looking good!"
            }



def main():
    logging.basicConfig(level=logging.INFO)
    session = ftrack_api.Session()

    action = checkShots(session)
    action.register()

    session.event_hub.wait()

if __name__ == "__main__":
    main()

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
2 2