Popular Content

Showing content with the highest reputation since 06/12/2020 in all areas

  1. 1 point
    import logging import ftrack_api import sys import pprint import ftrack_connect.application class ApplicationStore(ftrack_connect.application.ApplicationStore): '''Store used to find and keep track of available applications.''' def _discoverApplications(self): '''Return a list of applications that can be launched from this host.''' applications = [] if sys.platform == 'darwin': prefix = ['/', 'Applications'] applications.extend(self._searchFilesystem( expression=prefix + [ 'Blender*', 'Blender.app' ], label='Blender {version}', applicationIdentifier='blender_{version}' )) elif sys.platform == 'win32': prefix = ['C:\\', 'Program Files.*'] applications.extend(self._searchFilesystem( expression=( prefix + ['Blender Foundation', 'Blender*', 'blender.exe'] ), label='Blender {version}', applicationIdentifier='blender_{version}' )) self.logger.debug( 'Discovered applications:\n{0}'.format( pprint.pformat(applications) ) ) return applications class BlenderAction(object): '''Launch Blender action.''' # Unique action identifier. identifier = 'blender-launch-action' def __debug(self, message): f = open("C:\\Users\\aaron\\Desktop\\output.txt", "a") f.write(pprint.pformat(message)) f.close() def __init__(self, applicationStore, launcher): '''Initialise action with *applicationStore*.''' super(BlenderAction, self).__init__() self.logger = logging.getLogger( __name__ + '.' + self.__class__.__name__ ) self.applicationStore = applicationStore self.launcher = launcher if self.identifier is None: raise ValueError('The action must be given an identifier.') def register(self, session): '''Register action.''' session.event_hub.subscribe( 'topic=ftrack.action.discover', self.discover ) session.event_hub.subscribe( 'topic=ftrack.action.launch and data.actionIdentifier={0}'.format( self.identifier ), self.launch ) self.session = session def is_component(self, selection): if ( len(selection) != 1 or selection[0]['entityType'] != 'Component' ): return False return True def discover(self, event): '''Return action based on *event*.''' launchArguments = [] selection = event['data'].get('selection', []) if self.is_component(selection): component = self.session.get('Component', selection[0]['entityId']) if component is not None: location = self.session.pick_location() url = location.get_filesystem_path(component) launchArguments.append(url) items = [] applications = self.applicationStore.applications applications = sorted( applications, key=lambda application: application['label'] ) for application in applications: applicationIdentifier = application['identifier'] label = application['label'] items.append({ 'actionIdentifier': self.identifier, 'label': label, 'icon': 'http://icons.iconarchive.com/icons/dakirby309/simply-styled/256/Blender-icon.png', 'applicationIdentifier': applicationIdentifier, 'launchArguments': ['c:\\Users\\aaron\\Desktop\\lookdev_addon.blend'] #launchArguments }) return { 'items': items } def launch(self, event): '''Callback method for Blender action.''' applicationIdentifier = ( event['data']['applicationIdentifier'] ) context = event['data'].copy() return self.launcher.launch( applicationIdentifier, context ) class ApplicationLauncher(ftrack_connect.application.ApplicationLauncher): '''Custom launcher to modify environment before launch.''' def __debug(self, message): f = open("C:\\Users\\aaron\\Desktop\\output.txt", "a") f.write(pprint.pformat(message)) f.close() def _getApplicationLaunchCommand(self, application, context=None): command = ftrack_connect.application.ApplicationLauncher._getApplicationLaunchCommand(self, application, context) self.__debug(command) return command def register(session, **kw): '''Register action in Connect.''' # Validate that session is an instance of ftrack_api.Session. If not, assume # that register is being called from an old or incompatible API and return # without doing anything. if not isinstance(session, ftrack_api.Session): return applicationStore = ApplicationStore() launcher = ApplicationLauncher( applicationStore ) action = BlenderAction(applicationStore, launcher) action.register(session) Here's the full hook. To be honest, it's not too much different from the Houdini example in the docs at the moment, which is why I'm not sure what's going on. Let me know if something sticks out.
  2. 1 point
    Unfortunately there is no reliable way of detecting the machine from the browser, not in a way that can be re-created from python. I've made a small plugin (see code below) that detects if there are multiple instances of Connect running for a user and pops up a message. If this functionality makes sense and you like it, we may bundle the plugin in Connect. import logging import socket import functools from QtExt import QtWidgets, QtCore import ftrack_api import ftrack_connect.util import ftrack_connect.ui.application logger = logging.getLogger( 'multiple-instances-plugin' ) def open_warning_dialog(message): '''Open warning window.''' parent = None for item in QtCore.QCoreApplication.instance().topLevelWidgets(): if isinstance(item, ftrack_connect.ui.application.Application): parent = item dialog = QtWidgets.QMessageBox(parent=parent) dialog.setText(message) dialog.setIcon(QtWidgets.QMessageBox.Critical) dialog.exec_() def handle_reply(event): '''Handle reply event if someone else is already running Connect.''' logger.warning('Someone else is already running connect: {0}'.format(event)) ftrack_connect.util.invoke_in_main_thread( open_warning_dialog, u'ftrack Connect is already running on machine: {0}'.format( event['data']['machine'] ) ) def callback(event): '''Handle start of another connect.''' logger.warning('Someone just started ftrack Connect: {0}'.format(event)) ftrack_connect.util.invoke_in_main_thread( functools.partial( open_warning_dialog, u'Connect just opened on machine: {0}'.format( event['data']['machine'] ) ) ) return {'machine': socket.gethostname()} def register(session, **kw): '''Register hooks.''' logger.info( 'Register detection of multiple Connect instances' ) # Validate that session is an instance of ftrack_api.session.Session. If # not, assume that register is being called from an old or incompatible API # and return without doing anything. if not isinstance(session, ftrack_api.Session): logger.debug( 'Not subscribing plugin as passed argument {0!r} is not an ' 'Session instance.'.format(session) ) return session.event_hub.subscribe( 'topic=studio.ftrack-connect.start and source.id != "{0}" and ' 'data.username = "{1}"'.format( session.event_hub.id, session._api_user ), callback ) session.event_hub.publish( ftrack_api.event.base.Event( topic='studio.ftrack-connect.start', data={ 'username': session._api_user, 'machine': socket.gethostname() } ), on_reply=handle_reply ) To use it create a folder detect-multi-instance/hook/ inside the ftrack connectp plugins folder, and add the code in a plugin.py (don't forget the hook/ folder). Restart Connect for the plugin to source. This simple example could be extended with the ability to "quit" other instances of ftrack Connect.