Skip to main content

Reference selector widget example

This s the widget that will represent the maya reference collector plugin. The populatemethod is always called by the base Class, and the query_references is used to call the ui_hook method from the plugin to populate the UI. As the execution of the ui_hook is async, the result goes to the ui_hook_callback method. In the _on_reference_changed method we modify the config of the plugin to set the selected_reference option.

Please check the widget extension section to know more.


# :coding: utf-8
# :copyright: Copyright (c) 2014-2024 ftrack

from Qt import QtWidgets, QtCore

from ftrack_qt.utils.decorators import invoke_in_qt_main_thread

from ftrack_framework_qt.widgets import BaseWidget


class MayaReferenceSelectorWidget(BaseWidget):
'''Main class to represent a list of Maya node graph root nodes'''

name = 'maya_reference_selector'
ui_type = 'qt'

def __init__(
self,
event_manager,
client_id,
context_id,
plugin_config,
group_config,
on_set_plugin_option,
on_run_ui_hook,
parent=None,
):
self._references_cb = None

super(MayaReferenceSelectorWidget, self).__init__(
event_manager,
client_id,
context_id,
plugin_config,
group_config,
on_set_plugin_option,
on_run_ui_hook,
parent,
)

def pre_build_ui(self):
layout = QtWidgets.QHBoxLayout()
layout.setContentsMargins(0, 0, 0, 0)
layout.setAlignment(QtCore.Qt.AlignTop)
self.setLayout(layout)
self.setSizePolicy(
QtWidgets.QSizePolicy.Preferred, QtWidgets.QSizePolicy.Fixed
)

def build_ui(self):
'''build function widgets.'''

label = QtWidgets.QLabel("Current references :")
self._references_cb = QtWidgets.QComboBox()

# Add the widgets to the layout
self.layout().addWidget(label)
self.layout().addWidget(self._references_cb)


def post_build_ui(self):
'''hook events'''
self._references_cb.currentTextChanged.connect(self._on_reference_changed)

def populate(self):
'''Fetch info from plugin to populate the widget'''
self.query_references()

def query_references(self):
'''Query All references from scene.'''
# payload is used to pass options to the ui_hook
payload = {}
self.run_ui_hook(payload)

@invoke_in_qt_main_thread
def ui_hook_callback(self, ui_hook_result):
'''Handle the result of the UI hook.'''
super(MayaReferenceSelectorWidget, self).ui_hook_callback(ui_hook_result)
self._references_cb.addItems(ui_hook_result)
self._references_cb.setCurrentText(ui_hook_result[0])

def _on_reference_changed(self, reference):
'''Updates the camera_name option with the provided *camera_name'''
if not reference:
return
self.set_plugin_option('selected_reference', reference)