Jump to content

Ftrack actions (updating the events object)


Alican

Recommended Posts

I am working from the 'Example Action' found here.

http://ftrack.rtd.ftrack.com/en/3.5.0/developing/actions.htm

 

My end goal is to create directory structures based on Ftrack hiearchy. 

 

At the moment, all I am doing is printing some information (object type and name) based on what is selected.

Everything works... but when I change the name of the selected task, or its type through Ftrack, and when I run the action again, it returns the information from my selection prior to the changes made.

 

How can I force update the events inside of the launch() function so that it always operates on the latest ftrack information?

 

Here is a very basic version of my code:

import logging

import ftrack_api
import os

#os.environ['FTRACK_SERVER'] = ############
#os.environ['FTRACK_API_USER'] = os.environ.get("USERNAME")
#os.environ['FTRACK_API_KEY'] = ############


class MyCustomAction(object):
    '''Custom action.'''

    label = 'Create Folders'
    identifier = 'create.initial.folders'
    description = 'This creates the initial task folders'

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

    def register(self):
        '''Register action.'''
        self.session.event_hub.subscribe(
            'topic=ftrack.action.discover and source.user.username={0}'.format(
                os.environ.get("USERNAME")#self.session.api_user
            ),
            self.discover
        )

        self.session.event_hub.subscribe(
            'topic=ftrack.action.launch and data.actionIdentifier={0} and '
            'source.user.username={1}'.format(
                self.identifier,
                os.environ.get("USERNAME")#self.session.api_user
            ),
            self.launch
        )

    def discover(self, event):
        '''Return action config if triggered on a single asset version.'''
        data = event['data']

        # If selection contains more than one item return early since
        # this action can only handle a single version.
        selection = data.get('selection', [])
        self.logger.info('Got selection: {0}'.format(selection))
        #if len(selection) != 1 or selection[0]['entityType'] != 'assetversion':
        #    return

        return {
            'items': [{
                'label': self.label,
                'description': self.description,
                'actionIdentifier': self.identifier
            }]
        }

    def launch(self, event):
        ########## How can I update this ^^ event so that launch() doesnt operate on an outdated information
        ##########
        ##########
        ##########

        '''Callback method for custom action.'''
        selection = event['data'].get('selection', [])

        #print selection

        for entity in selection:

            item = self.session.get('Task', entity['entityId']) 
            object_type = item['object_type']['name']
            name = item['name']
            print name, object_type

        return {
            'success': True,
            'message': 'Ran my custom action successfully!'
        }

        
def register(session, **kw):
    '''Register plugin.'''

    # Validate that session is an instance of ftrack_api.Session. If not,
    # assume that register is being called from an incompatible API
    # and return without doing anything.
    if not isinstance(session, ftrack_api.Session):
        # Exit to avoid registering this plugin again.
        return

    action = MyCustomAction(session)
    action.register()

if __name__ == '__main__':
    logging.basicConfig(level=logging.INFO)
    session = ftrack_api.Session()
    register(session)

    # Wait for events.
    session.event_hub.wait()

 

Thanks!

Ali

Link to comment
Share on other sites

Hi, when you access the task the second time it will already be cached in memory (along with the name) by the API. But if you explicitly fetch it with the appropriate projections every time you should get fresh data:

item = session.query('select name from Task where id is {0}'.format(entity['entityId'])).one()

Make sure to include the projections (name) as that otherwise will be cached.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...